37 Commits

Author SHA1 Message Date
星光-k
1ac9ca91ef Update weather.py 2025-05-09 00:46:43 +08:00
星光-k
5c8d24f43c Update main.py 2025-05-09 00:46:14 +08:00
星光-k
13adf24df4 Update weather.py 2025-03-15 11:01:52 +08:00
星光-k
09c6c3c7ac Update README.md 2025-02-08 20:15:29 +08:00
星光-k
caf0228277 Update README.md 2025-02-08 19:17:44 +08:00
星光-k
54b76b4d26 Update install.sh 2025-02-08 18:56:22 +08:00
星光-k
f25f0d8dcf Update install.sh 2025-02-08 15:01:20 +08:00
星光-k
8f0fd2e6d6 Delete bin/vendor/1 2025-02-08 14:59:05 +08:00
星光-k
7350d120ad Add files via upload 2025-02-08 06:58:26 +00:00
星光-k
db8c4344ba Create 1 2025-02-08 14:57:23 +08:00
星光-k
7759cabcf0 Update install.sh 2025-02-08 13:56:42 +08:00
星光-k
a56b337975 Rename requirements.txt to bin/requirements.txt 2025-02-08 13:50:06 +08:00
星光-k
7cd6136696 Add files via upload 2025-02-08 05:49:03 +00:00
星光-k
569852a1dc 更新 weather.py 2025-02-08 01:17:52 +00:00
星光-k
7c48d92bc1 更新 main.py 2025-02-08 00:59:44 +00:00
星光-k
9b3afeb323 Update start.sh 2025-01-27 21:52:38 +08:00
星光-k
2d3459f3d0 更新 clock.py 2025-01-23 15:21:54 +00:00
星光-k
c05eb73fb6 更新 main.py1 2025-01-23 15:21:34 +00:00
星光-k
b9cd0bab9d 更新 main.py 2025-01-23 15:20:47 +00:00
星光-k
db174e366d Update main.py 2025-01-23 12:30:07 +08:00
星光-k
0c4f04e612 Update clock.py 2025-01-23 12:29:23 +08:00
星光-k
a0f636a8d5 Update main.py 2025-01-23 12:26:28 +08:00
星光-k
d0fe962c06 Update clock.py 2025-01-23 11:35:51 +08:00
星光-k
8d3bb91ed3 Update weather.py 2025-01-23 11:10:45 +08:00
星光-k
9d69de92f2 Update main.py 2025-01-23 08:30:19 +08:00
星光-k
77fef0187e Add files via upload 2025-01-22 05:41:51 +00:00
星光-k
19177a14ba 更新 main.py 2025-01-21 16:23:15 +00:00
星光-k
6d7fdab6e1 更新 main.py 2025-01-21 15:46:36 +00:00
星光-k
3842f61add 更新 main.py 2025-01-21 14:08:26 +00:00
星光-k
f1a2e59e4b 更新 main.py 2025-01-21 13:46:07 +00:00
星光-k
b8c1df3ee2 Update README.md 2025-01-21 13:46:38 +08:00
星光-k
da6b99e99c Add files via upload 2025-01-21 05:46:11 +00:00
星光-k
01ea753627 Add files via upload 2025-01-21 05:43:27 +00:00
星光-k
4c3fefa9ff Update README.md 2025-01-21 13:41:02 +08:00
星光-k
c7275aa400 Update README.md 2025-01-21 13:40:47 +08:00
星光-k
a1e9356c83 Update README.md 2025-01-21 13:40:27 +08:00
星光-k
914129a181 Update main.py 2025-01-21 13:38:15 +08:00
14 changed files with 690 additions and 336 deletions

View File

@@ -1,6 +1,6 @@
# 墨水屏展示当前时间及天气数据 # 墨水屏展示当前时间及天气数据
# 此分支专属于本人的个人设备实验分支以及备份
本项目复刻自[Seek-Huang](https://github.com/Seek-Huang/2.13-Ink-screen-clock)的代码仓库 本项目复刻自[Seek-Huang](https://github.com/Seek-Huang)的[代码仓库](https://github.com/Seek-Huang/2.13-Ink-screen-clock)
并在此基础上进行改进 并在此基础上进行改进
## 本仓库已添加[一键安装部署脚本](https://github.com/kxgx/2.13-Ink-screen-clock#%E4%BD%BF%E7%94%A8%E8%84%9A%E6%9C%AC%E7%9B%B4%E6%8E%A5%E5%AE%89%E8%A3%85%E6%8E%A8%E8%8D%90) ## 本仓库已添加[一键安装部署脚本](https://github.com/kxgx/2.13-Ink-screen-clock#%E4%BD%BF%E7%94%A8%E8%84%9A%E6%9C%AC%E7%9B%B4%E6%8E%A5%E5%AE%89%E8%A3%85%E6%8E%A8%E8%8D%90)

View File

@@ -10,37 +10,32 @@ from threading import Timer
import requests import requests
white = 255 #颜色 white = 255 #颜色
black = 0 black = 0
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.DEBUG)
################################引入配置文件开始################################################ ################################引入配置文件开始################################################
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic') picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib') libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir): if os.path.exists(libdir):
sys.path.append(libdir) # 将引入文件添加到环境变量 sys.path.append(libdir)#将引入文件添加到环境变量
from waveshare_epd import epd2in13_V4 # 引入墨水屏驱动文件 from waveshare_epd import epd2in13_V4 #引入墨水屏驱动文件
logging.debug("Loading Fonts") logging.debug("Loading Fonts")
font01 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20) # 字体文件 font01 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20) #字体文件
font02 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15) # 字体文件 font02 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15) #字体文件
font03 = ImageFont.truetype(os.path.join(picdir, 'DSEG7Modern-Bold.ttf'), 66) # 字体文件 font03 = ImageFont.truetype(os.path.join(picdir, 'DSEG7Modern-Bold.ttf'), 66) #字体文件
font04 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 10) # 字体文件 font04 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 10) #字体文件
font05 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 12) # 字体文件 font05 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 12) #字体文件
font06 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 13) # 字体文件 font06 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 13) #字体文件
################################引入配置文件结束################################################ ################################引入配置文件结束################################################
def Local_strong_brush(): #局部强制刷新显示
i = 0
def Local_strong_brush(): # 局部强制刷新显示 while i < 5:
i = 0 epd.displayPartial(epd.getbuffer(info_image.rotate(180)))#局刷开始
while i < 5: i = i + 1
epd.displayPartial(epd.getbuffer(info_image.rotate(180))) # 局刷开始 def get_date():#返回当前年月日及星期几
i += 1
def get_date(): # 返回当前年月日及星期几
date = datetime.datetime.now() date = datetime.datetime.now()
today = LunarDate.today() today=LunarDate.today()
week_day_dict = {0: '星期一', 1: '星期二', 2: '星期三', 3: '星期四', 4: '星期五', 5: '星期六', 6: '星期日'} week_day_dict = {0: '星期一',1: '星期二',2: '星期三',3: '星期四',4: '星期五',5: '星期六',6: '星期日',}
day = date.weekday() day = date.weekday()
return time.strftime('%Y年%m月%d') + '' + week_day_dict[day] + '' + today.strftime('农历%M月%D') return time.strftime('%Y年%m月%d')+''+week_day_dict[day]+''+today.strftime('农历%M月%D')
# 定义一个全局标志变量,用于检查是否已经设置了系统时间或尝试过设置 # 定义一个全局标志变量,用于检查是否已经设置了系统时间或尝试过设置
has_set_system_time = False has_set_system_time = False
@@ -79,7 +74,7 @@ def set_system_time_from_hwclock(utc=True):
# 检查时间是否发生了倒退 # 检查时间是否发生了倒退
if after_time < before_time: if after_time < before_time:
logging.info(f"Time went backwards after hwclock call: before={before_time}, after={after_time}") logging.warning(f"Time went backwards after hwclock call: before={before_time}, after={after_time}")
return True return True
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
@@ -102,106 +97,92 @@ def get_time():
current_time = time.strftime('%H:%M').upper() current_time = time.strftime('%H:%M').upper()
logging.debug(f"Returning current time: {current_time}") logging.debug(f"Returning current time: {current_time}")
return current_time return current_time
def Get_ipv4_address(): # 获取当前的IP地址
def Get_address(): # 获取当前的IP地址 try:
return subprocess.check_output(u"hostname -I | cut -d' ' -f1 | head --bytes -1", shell=True).decode('gbk').strip() ip_output = subprocess.check_output(
"hostname -I | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}'", shell=True).decode('utf-8').strip()
ip_list = ip_output.split()
def CPU_temperature(): # CPU温度获取 filtered_ips = [ip for ip in ip_list if not ip.startswith("172.")]
temperatura = os.popen('vcgencmd measure_temp').readline() return filtered_ips[0] if filtered_ips else "地址获取失败"
temperatura = temperatura.replace('temp=', '').strip() except subprocess.CalledProcessError:
return str(temperatura) return "获取失败"
def CPU_temperature():#CPU温度获取
temperatura = os.popen('vcgencmd measure_temp').readline()
def Memory_footprint(): # 显示内存占用百分比 temperatura = temperatura.replace('temp=','').strip()
return subprocess.check_output(u"free -m | awk -F '[ :]+' 'NR==2{printf \"%d\", ($3)/$2*100}'", shell=True).decode('gbk') return str(temperatura)
def Memory_footprint():#显示内存占用百分比
return(subprocess.check_output(u"free -m | awk -F '[ :]+' 'NR==2{printf \"%d\", ($3)/$2*100}'", shell = True ).decode('gbk'))
def CPU_usage(): # 显示CPU占用百分比 def CPU_usage(): #显示CPU占用百分比
return str(int(float(os.popen("top -b -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))) return(str(int(float(os.popen("top -b -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))))
def power_battery():#获取当前电池电量
return(str(int(subprocess.check_output(u"echo \"get battery\" | nc -q 0 127.0.0.1 8423|awk -F':' '{print int($2)}'", shell = True ).decode('gbk')))+u'%')
def power_battery(): # 获取当前电池电量
return str(int(subprocess.check_output(u"echo \"get battery\" | nc -q 0 127.0.0.1 8423|awk -F':' '{print int($2)}'", shell=True).decode('gbk'))) + u'%'
# 打印电量信息 # 打印电量信息
print(power_battery()) print(power_battery())
def Bottom_edge(): #在图片中添加底边内容
draw.rectangle((0, 105, 250, 122), 'black', 'black')
def Bottom_edge(): # 在图片中添加底边内容 '''电池图标画图'''
draw.rectangle((0, 105, 250, 122), 'black', 'black') draw.line((126,109,154,109),fill=255, width=1) #电池顶边
'''电池图标画图''' draw.line((126,110,126,119),fill=255, width=1) #电池左边
draw.line((126, 109, 154, 109), fill=255, width=1) # 电池 draw.line((127,119,154,119),fill=255, width=1) #电池
draw.line((126, 110, 126, 119), fill=255, width=1) # 电池左边 draw.line((154,110,154,118),fill=255, width=1)
draw.line((127, 119, 154, 119), fill=255, width=1) # 电池下边 draw.line((155,112,157,112),fill=255, width=1)
draw.line((154, 110, 154, 118), fill=255, width=1) draw.line((155,116,157,116),fill=255, width=1)
draw.line((155, 112, 157, 112), fill=255, width=1) draw.line((157,113,157,115),fill=255, width=1)
draw.line((155, 116, 157, 116), fill=255, width=1) global power_str
draw.line((157, 113, 157, 115), fill=255, width=1) power_str=power_battery()
global power_str draw.text((128,108),power_str,font = font04,fill =255) #显示当前电量百分比
power_str = power_battery() '''电池图标画图'''
draw.text((128, 108), power_str, font=font04, fill=255) # 显示当前电量百分比 draw.ellipse((192, 107, 207, 120), 0, 255)# 时钟图标
'''电池图标画图''' draw.line((199,109,199,114),fill=255, width=1)
draw.ellipse((192, 107, 207, 120), 0, 255) # 时钟图标 draw.line((200,114,204,114),fill=255, width=1)
draw.line((199, 109, 199, 114), fill=255, width=1) global local_addr #获取当前IP地址
draw.line((200, 114, 204, 114), fill=255, width=1) local_addr= Get_ipv4_address() #获取当前IP地址
global local_addr # 获取当前IP地址 draw.text((10,107),"IP:"+local_addr,font = font05,fill =255)#显示当前IP地址
local_addr = Get_address() # 获取当前IP地址 def Basic_refresh(): #全刷函数
draw.text((10, 107), "IP:" + local_addr, font=font05, fill=255) # 显示当前IP地址 logging.info("Refresh and prepare the basic content before starting the canvas")#开始画布前刷新准备基础内容
def Basic_refresh(): # 全刷函数
logging.info("Refresh and prepare the basic content before starting the canvas") # 开始画布前刷新准备基础内容
global get_date_var global get_date_var
get_date_var = get_date() # 记录开始数据 get_date_var=get_date() #记录开始数据
draw.text((2, 2), get_date_var, font=font02, fill=0) # 将日期及星期几显示到屏幕 draw.text((2,2),get_date_var,font = font02,fill =0)#将日期及星期几显示到屏幕
global local_time global local_time
local_time = get_time() local_time=get_time()
draw.text((5, 28), local_time, font=font03, fill=0) # 显示当前时间 draw.text((5,40),local_time,font = font03,fill =0)#显示当前时间
Bottom_edge() # 添加底边内容 Bottom_edge() #添加底边内容
epd.display(epd.getbuffer(info_image.rotate(180))) epd.display(epd.getbuffer(info_image.rotate(180)))
def Partial_full_brush(): #局部定时全刷函数
Basic_refresh() #全局刷新
def Partial_full_brush(): # 局部定时全刷函数 logging.debug("局部定时全局刷新")
Basic_refresh() # 全局刷新 epd.init()
logging.debug("局部定时全局刷新") def Partial_refresh():#局刷函数
epd.init() logging.info("Partial content update, this update is recommended to be synchronized with the minute to save the life of the ink screen")#局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命
epd.displayPartBaseImage(epd.getbuffer(info_image.rotate(180)))
epd.init()
def Partial_refresh(): # 局刷函数 while (True):
logging.info( global local_time
"Partial content update, this update is recommended to be synchronized with the minute to save the life of the ink screen") # 局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命 local_time1=get_time()
epd.displayPartBaseImage(epd.getbuffer(info_image.rotate(180))) if (local_time1==local_time) ==False:
epd.init() draw.rectangle((5, 17, 133, 95), fill = 255) #时间局刷区域
while True: draw.text((5,17),local_time1,font = font03,fill =0)#刷新当前时间
global local_time local_time=local_time1
local_time1 = get_time() Local_strong_brush() #局部强刷
if local_time1 != local_time: get_date_var1=get_date() #局刷判断,如果时间与前一次不一致说明内容变化,需要刷新显示
draw.rectangle((5, 28, 133, 95), fill=255) # 时间局刷区域 global get_date_var #再次声明这个是全局变量
draw.text((5, 28), local_time1, font=font03, fill=0) # 刷新当前时间 if(get_date_var1==get_date_var) ==False:
local_time = local_time1 draw.rectangle((2, 2, 250, 16), fill = 255) #设置头部刷新区域
Local_strong_brush() # 局部强刷 draw.text((2,2),get_date_var1,font = font02,fill =0)#将日期及星期几刷新显示到屏幕
get_date_var1 = get_date() # 局刷判断,如果时间与前一次不一致说明内容变化,需要刷新显示 get_date_var=get_date_var1 #将更新的值保存到初始变量,直到下一次变化时执行该刷新操作
global get_date_var # 再次声明这个是全局变量 logging.debug("头部日期部位发生刷新变化.")
if get_date_var1 != get_date_var: Local_strong_brush() #局部强刷
draw.rectangle((2, 2, 250, 16), fill=255) # 设置头部刷新区域 global local_addr #当前IP地址
draw.text((2, 2), get_date_var1, font=font02, fill=0) # 将日期及星期几刷新显示到屏幕 local_addr1 = Get_ipv4_address()
get_date_var = get_date_var1 # 将更新的值保存到初始变量,直到下一次变化时执行该刷新操作 if (local_addr1==local_addr) ==False:
logging.debug("头部日期部位发生刷新变化.") draw.rectangle((1, 107, 94, 120), fill = 0) #设置头部刷新区域
Local_strong_brush() # 局部强刷 draw.text((10,107),"IP:"+local_addr1,font = font05,fill =255)#显示当前IP地址
global local_addr # 当前IP地址 local_addr=local_addr1
local_addr1 = Get_address() Local_strong_brush() #局部强刷
if local_addr1 != local_addr: #显示当前电量百分比
draw.rectangle((1, 107, 94, 120), fill=0) # 设置头部刷新区域 power_str=power_str1
draw.text((10, 107), "IP:" + local_addr1, font=font05, fill=255) # 显示当前IP地址 Local_strong_brush() #局部强刷
local_addr = local_addr1 logging.debug("电源电量局部刷新")
Local_strong_brush() # 局部强刷
# 显示当前电量百分比
global power_str
power_str1 = power_battery()
power_str = power_str1
Local_strong_brush() # 局部强刷
retry_interval = 180 # 设置重试间隔时间(秒) retry_interval = 180 # 设置重试间隔时间(秒)
while True: while True:

View File

@@ -1,35 +1,37 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
from PIL import Image, ImageDraw, ImageFont # 引入图片处理库 from PIL import Image,ImageDraw,ImageFont #引入图片处理库
import os, sys, json, time, datetime # 引入系统相关库 import os,sys,re,json,time,datetime #引入系统相关库
from borax.calendars.lunardate import LunarDate # 农历日期以及天干地支纪年法的 Python 库 from borax.calendars.lunardate import LunarDate #农历日期以及天干地支纪年法的 Python 库
import logging # 日志库 import logging #日志库
import subprocess import subprocess
import os
from threading import Timer
import requests
import socket
white = 255 # 颜色 white = 255 #颜色
black = 0 black = 0
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s') logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
################################引入配置文件开始################################################ ################################引入配置文件开始################################################
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic') picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib') libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir): if os.path.exists(libdir):
sys.path.append(libdir) # 将引入文件添加到环境变量 sys.path.append(libdir)#将引入文件添加到环境变量
from waveshare_epd import epd2in13_V4 # 引入墨水屏驱动文件 from waveshare_epd import epd2in13_V4 #引入墨水屏驱动文件
logging.debug("Loading Fonts") logging.debug("Loading Fonts")
font01 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20) #字体文件 font01 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20) #字体文件
font02 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 15) #字体文件 font02 = ImageFont.truetype(os.path.join(picdir, 'GB2312.ttf'), 15) #字体文件
font03 = ImageFont.truetype(os.path.join(picdir, 'DSEG7Modern-Bold.ttf'), 38) #字体文件 font03 = ImageFont.truetype(os.path.join(picdir, 'Fonttt.ttf'), 48) #字体文件
font04 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 10) #字体文件 font04 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 10) #字体文件
font05 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 12) #字体文件 font05 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 12) #字体文件
font06 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 13) #字体文件 font06 = ImageFont.truetype(os.path.join(picdir, '原神cn.ttf'), 13) #字体文件
################################引入配置文件结束################################################ ################################引入配置文件结束################################################
def Local_strong_brush(): #局部强制刷新显示
i = 0
def Local_strong_brush(): # 局部强制刷新显示 while i < 5:
for _ in range(5): epd.displayPartial(epd.getbuffer(info_image.rotate(180)))#局刷开始
epd.displayPartial(epd.getbuffer(info_image.rotate(180))) i = i + 1
def get_date(): # 返回当前年月日及星期几 def get_date(): # 返回当前年月日及星期几
date = datetime.datetime.now() date = datetime.datetime.now()
today = LunarDate.today() today = LunarDate.today()
@@ -137,142 +139,161 @@ def power_battery(): # 获取当前电池电量
new_power = last_power # 未到更新时间,使用缓存值 new_power = last_power # 未到更新时间,使用缓存值
return new_power return new_power
def Bottom_edge(): # 在图片中添加底边内容 # 打印电量信息
draw.rectangle((0, 105, 250, 122), 'black', 'black') print(power_battery())
'''电池图标画图''' def Bottom_edge(): #在图片中添加底边内容
draw.line((126, 109, 154, 109), fill=255, width=1) # 电池顶边 draw.rectangle((0, 105, 250, 122), 'black', 'black')
draw.line((126, 110, 126, 119), fill=255, width=1) # 电池左边 '''电池图标画图'''
draw.line((127, 119, 154, 119), fill=255, width=1) # 电池 draw.line((126,109,154,109),fill=255, width=1) #电池
draw.line((154, 110, 154, 118), fill=255, width=1) draw.line((126,110,126,119),fill=255, width=1) #电池左边
draw.line((155, 112, 157, 112), fill=255, width=1) draw.line((127,119,154,119),fill=255, width=1) #电池下边
draw.line((155, 116, 157, 116), fill=255, width=1) draw.line((154,110,154,118),fill=255, width=1)
draw.line((157, 113, 157, 115), fill=255, width=1) draw.line((155,112,157,112),fill=255, width=1)
global power_str draw.line((155,116,157,116),fill=255, width=1)
power_str = power_battery() draw.line((157,113,157,115),fill=255, width=1)
draw.text((129, 108), power_str, font=font04, fill=255) # 显示当前电量百分比 global power_str
'''电池图标画图''' power_str=power_battery()
draw.ellipse((192, 107, 207, 120), 0, 255) # 时钟图标 draw.text((129,108),power_str,font = font04,fill =255) #显示当前电量百分比
draw.line((199, 109, 199, 114), fill=255, width=1) '''电池图标画图'''
draw.line((200, 114, 204, 114), fill=255, width=1) draw.ellipse((192, 107, 207, 120), 0, 255)# 时钟图标
global local_addr # 获取当前IP地址 draw.line((199,109,199,114),fill=255, width=1)
local_addr = Get_ipv4_address() # 获取当前IP地址 draw.line((200,114,204,114),fill=255, width=1)
draw.text((10, 107), f"IP:{local_addr}", font=font05, fill=255) # 显示当前IP地址 global local_addr #获取当前IP地址
local_addr= Get_ipv4_address() #获取当前IP地址
draw.text((10,107),"IP:"+local_addr,font = font05,fill =255)#显示当前IP地址
def Weather(): # 在图片中添加天气内容 def Weather():
with open('/root/2.13-Ink-screen-clock/bin/weather.json', 'r') as file: try:
weather_data = json.load(file) with open('/root/2.13-Ink-screen-clock/bin/weather.json', 'r') as Weather_json:
global Weather_position, temperature, weather, wind_direction, weather_update, weather_date, humidity Weather_data = Weather_json.read()
Weather_position = weather_data['cityname'] # 定位位置 if not Weather_data.strip(): # 检查文件是否为空
temperature = weather_data['temp'] + u'°C' # 温度 logging.error("天气数据文件为空")
weather = weather_data['weather'] # 天气情况 return
wind_direction = weather_data['WD'] # 风向
weather_update = weather_data['time'] # 天气更新时间 Weather_text = json.loads(Weather_data)
weather_date = weather_data['date'] # 日期 global Weather_position, temperature, weather, wind_direction, weather_update, weather_date, humidity
humidity = weather_data['SD'] # 湿度
draw.text((150, 25), "天气:", font=font06, fill=0) # 显示当前天气前缀 Weather_position = Weather_text.get('cityname', '未知') # 使用get方法提供默认值
draw.text((150, 45), "温度:", font=font06, fill=0) # 显示当前温度前缀 temperature = f"{Weather_text.get('temp', '--')}°C"
draw.text((150, 65), "湿度:", font=font06, fill=0) # 显示当前湿度前缀 weather = Weather_text.get('weather', '未知')
draw.text((150, 85), "城市:", font=font06, fill=0) # 显示当前城市前缀 wind_direction = Weather_text.get('WD', '未知')
draw.text((191, 25), weather, font=font06, fill=0) weather_update = Weather_text.get('time', '未知')
draw.text((191, 45), temperature, font=font06, fill=0) weather_date = Weather_text.get('date', '未知')
draw.text((191, 65), humidity, font=font06, fill=0) humidity = Weather_text.get('SD', '未知')
draw.text((191, 85), Weather_position, font=font06, fill=0) draw.text((150,25),"天气:",font = font06,fill =0)#显示当前天气前缀
draw.text((211, 107), weather_update, font=font05, fill=255) # 显示天气更新时间 draw.text((150,45),"温度:",font = font06,fill =0)#显示当前温度前缀
draw.text((150,65),"湿度:",font = font06,fill =0)#显示当前湿度前缀
draw.text((150,85),"城市:",font = font06,fill =0)#显示当前城市前缀
def Basic_refresh(): # 全刷函数 draw.text((191,25),weather,font = font06,fill =0)
logging.info("在启动画布之前,刷新并准备基本内容") # 开始画布前刷新准备基础内容 draw.text((191,45),temperature,font = font06,fill =0)
draw.text((191,65),humidity,font = font06,fill =0)
draw.text((191,85),Weather_position,font = font06,fill =0)
draw.text((211,107),weather_update,font = font05,fill =255) #显示天气更新时间
except FileNotFoundError:
logging.error("天气数据文件未找到")
except json.JSONDecodeError as e:
logging.error(f"天气数据解析失败: {str(e)}")
except Exception as e:
logging.error(f"获取天气信息时发生错误: {str(e)}")
def Basic_refresh(): #全刷函数
logging.info("在启动画布之前,刷新并准备基本内容")#开始画布前刷新准备基础内容
global get_date_var global get_date_var
get_date_var = get_date() # 记录开始数据 get_date_var=get_date() #记录开始数据
draw.text((2, 2), get_date_var, font=font02, fill=0) # 将日期及星期几显示到屏幕 draw.text((2,2),get_date_var,font = font02,fill =0)#将日期及星期几显示到屏幕
global local_time global local_time
local_time = get_time() local_time=get_time()
draw.text((5, 40), local_time, font=font03, fill=0) # 显示当前时间 draw.text((5,28),local_time,font = font03,fill =0)#显示当前时间
Bottom_edge() # 添加底边内容 Bottom_edge() #添加底边内容
Weather() # 天气内容 Weather() #天气内容
epd.display(epd.getbuffer(info_image.rotate(180))) epd.display(epd.getbuffer(info_image.rotate(180)))
def Partial_full_brush(): #局部定时全刷函数
Basic_refresh() #全局刷新
def Partial_full_brush(): # 局部定时全刷函数 logging.debug("局部定时全局刷新")
Basic_refresh() # 全局刷新 epd.init()
logging.debug("局部定时全局刷新") def Partial_refresh():#局刷函数
logging.info("部分内容更新,此更新建议与分钟同步,以节省墨水屏的使用寿命")#局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命
epd.displayPartBaseImage(epd.getbuffer(info_image.rotate(180)))
def Partial_refresh(): # 局刷函数 epd.init()
logging.info("部分内容更新,此更新建议与分钟同步,以节省墨水屏的使用寿命") # 局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命 while (True):
epd.displayPartBaseImage(epd.getbuffer(info_image.rotate(180))) global local_time
epd.init() local_time1=get_time()
while True: if (local_time1==local_time) ==False:
global local_time draw.rectangle((5, 28, 149, 82), fill = 255) #时间局刷区域
local_time1 = get_time() draw.text((5,28),local_time1,font = font03,fill =0)#刷新当前时间
if local_time1 != local_time: local_time=local_time1
draw.rectangle((5, 40, 133, 82), fill=255) # 时间局刷区域 Local_strong_brush() #局部强刷
draw.text((5, 40), local_time1, font=font03, fill=0) # 刷新当前时间 get_date_var1=get_date() #局刷判断,如果时间与前一次不一致说明内容变化,需要刷新显示
local_time = local_time1 global get_date_var #再次声明这个是全局变量
Local_strong_brush() # 局部强刷 if(get_date_var1==get_date_var) ==False:
get_date_var1 = get_date() # 局刷判断,如果时间与前一次不一致说明内容变化,需要刷新显示 draw.rectangle((2, 2, 250, 16), fill = 255) #设置头部刷新区域
global get_date_var # 再次声明这个是全局变量 draw.text((2,2),get_date_var1,font = font02,fill =0)#将日期及星期几刷新显示到屏幕
if get_date_var1 != get_date_var: get_date_var=get_date_var1 #将更新的值保存到初始变量,直到下一次变化时执行该刷新操作
draw.rectangle((2, 2, 250, 16), fill=255) # 设置头部刷新区域 logging.debug("头部日期部位发生刷新变化.")
draw.text((2, 2), get_date_var1, font=font02, fill=0) # 将日期及星期几刷新显示到屏幕 Local_strong_brush() #局部强刷
get_date_var = get_date_var1 # 将更新的值保存到初始变量,直到下一次变化时执行该刷新操作 global local_addr #当前IP地址
logging.debug("头部日期部位发生刷新变化.") local_addr1 = Get_ipv4_address()
Local_strong_brush() # 局部强刷 if (local_addr1==local_addr) ==False:
global local_addr # 当前IP地址 draw.rectangle((1, 107, 123, 120), fill = 0) #设置头部刷新区域
local_addr1 = Get_ipv4_address() draw.text((10,107),"IP:"+local_addr1,font = font05,fill =255)#显示当前IP地址
if local_addr1 != local_addr: local_addr=local_addr1
draw.rectangle((1, 107, 123, 120), fill=0) # 设置头部刷新区域 Local_strong_brush() #局部强刷
draw.text((10, 107), f"IP:{local_addr1}", font=font05, fill=255) # 显示当前IP地址 '''天气局部更新函数'''
local_addr = local_addr1 Weather_json = open('/root/2.13-Ink-screen-clock/bin/weather.json','r')
Local_strong_brush() # 局部强刷 Weather_data = Weather_json.read()
'''天气局部更新函数''' Weather_json.close()
with open('/root/2.13-Ink-screen-clock/bin/weather.json', 'r') as file: Weather_text=json.loads(Weather_data)
weather_data = json.load(file) global Weather_position
global Weather_position, temperature, weather, wind_direction, weather_update, weather_date, humidity global temperature
Weather_position1 = weather_data['cityname'] # 定位位置 global weather
temperature1 = weather_data['temp'] + u'°C' # 温度 global wind_direction
weather11 = weather_data['weather'] # 天气情况 global weather_update
humidity1 = weather_data['SD'] # 湿度 global weather_date
weather_update1 = weather_data['time'] # 天气更新时间 global humidity
if weather11 != weather: Weather_position1 = Weather_text['cityname'] #定位位置
draw.rectangle((191, 25, 249, 38), fill=255) # 天气局刷区域 temperature1=Weather_text['temp']+u'°C' #温度
draw.text((191, 25), weather11, font=font06, fill=0) weather11 = Weather_text['weather'] #天气情况
weather = weather11 wind_direction1 = Weather_text['WD'] #风向
logging.info("天气局部刷新") weather_update1 = Weather_text['time'] #天气更新时间
Local_strong_brush() # 局部强刷 weather_date1 = Weather_text['date'] #日期
if temperature1 != temperature: humidity1 = Weather_text['SD'] #湿度
draw.rectangle((191, 45, 249, 57), fill=255) # 局刷区域 if (weather11==weather) ==False:
draw.text((191, 45), temperature1, font=font06, fill=0) draw.rectangle((191, 25, 249, 38), fill = 255) #天气局刷区域
temperature = temperature1 draw.text((191,25),weather11,font = font06,fill =0)
logging.info("温度局部刷新") weather=weather11
Local_strong_brush() # 局部强刷 logging.info("天气局部刷新")
if humidity1 != humidity: Local_strong_brush() #局部强刷
draw.rectangle((191, 65, 249, 77), fill=255) # 局刷区域 if (temperature1==temperature) ==False:
draw.text((191, 65), humidity1, font=font06, fill=0) draw.rectangle((191, 45, 249, 57), fill = 255) #局刷区域
humidity = humidity1 draw.text((191,45),temperature1,font = font06,fill =0)
logging.info("湿度局部刷新") temperature=temperature1
Local_strong_brush() # 局部强刷 logging.info("温度局部刷新")
if Weather_position1 != Weather_position: Local_strong_brush() #局部强刷
draw.rectangle((191, 85, 249, 98), fill=255) # 局刷区域 if (humidity1==humidity) ==False:
draw.text((191, 85), Weather_position1, font=font06, fill=0) draw.rectangle((191, 65, 249, 77), fill = 255) #局刷区域
Weather_position = Weather_position1 draw.text((191,65),humidity1,font = font06,fill =0)
logging.info("城市局部刷新") humidity = humidity1
Local_strong_brush() # 局部强刷 logging.info("湿度局部刷新")
if weather_update1 != weather_update: Local_strong_brush() #局部强刷
draw.rectangle((211, 107, 248, 118), fill=0) # 设置更新时间刷新区域 if (Weather_position1==Weather_position) ==False:
draw.text((211, 107), weather_update1, font=font05, fill=255) # 显示天气更新时间 draw.rectangle((191, 85, 249, 98), fill = 255) #局刷区域
weather_update = weather_update1 draw.text((191,85),Weather_position1,font = font06,fill =0)
logging.info("天气更新时间局部刷新") Weather_position = Weather_position1
Local_strong_brush() # 局部强刷 logging.info("城市局部刷新")
'''天气局部更新函数''' Local_strong_brush() #局部强刷
global power_str if (weather_update1==weather_update) ==False:
power_str1 = power_battery() draw.rectangle((211, 107, 248, 118), fill = 0) #设置更新时间刷新区域
if power_str1 != power_str: draw.text((211,107),weather_update1,font = font05,fill =255) #显示天气更新时间
draw.rectangle((128, 110, 153, 117), fill=0) # 设置更新时间刷新区域 weather_update=weather_update1
draw.text((129, 108), power_str1, font=font04, fill=255) # 显示当前电量百分比 logging.info("天气更新时间局部刷新")
power_str = power_str1 Local_strong_brush() #局部强刷
Local_strong_brush() # 局部强刷 '''天气局部更新函数'''
global power_str
power_str1 =power_battery()
if (power_str1==power_str) ==False:
draw.rectangle((128, 110, 153, 117), fill = 0) #设置更新时间刷新区域
draw.text((129,108),power_battery(),font = font04,fill =255) #显示当前电量百分比
power_str=power_str1
Local_strong_brush() #局部强刷
# logging.info("电池电量局部刷新")
retry_interval = 180 # 设置重试间隔时间(秒) retry_interval = 180 # 设置重试间隔时间(秒)
while True: while True:
@@ -309,4 +330,4 @@ epd.init()
epd.Clear(0xFF) # 清除屏幕内容 epd.Clear(0xFF) # 清除屏幕内容
epd.sleep() # 使屏幕进入休眠状态 epd.sleep() # 使屏幕进入休眠状态
epd2in13_V4.epdconfig.module_exit() # 清理资源 epd2in13_V4.epdconfig.module_exit() # 清理资源
exit() exit()

258
bin/main.py1 Normal file
View File

@@ -0,0 +1,258 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from PIL import Image,ImageDraw,ImageFont #引入图片处理库
import os,sys,re,json,time,datetime #引入系统相关库
from borax.calendars.lunardate import LunarDate #农历日期以及天干地支纪年法的 Python 库
import logging #日志库
import subprocess
import os
from threading import Timer
import requests
import socket
white = 255 #颜色
black = 0
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
################################引入配置文件开始################################################
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
sys.path.append(libdir)#将引入文件添加到环境变量
from waveshare_epd import epd2in13_V4 #引入墨水屏驱动文件
logging.debug("Loading Fonts")
font01 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 20) #字体文件
font02 = ImageFont.truetype(os.path.join(picdir, 'GB2312.ttf'), 15) #字体文件
font03 = ImageFont.truetype(os.path.join(picdir, 'Fonttt.ttf'), 48) #字体文件
font04 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 10) #字体文件
font05 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 12) #字体文件
font06 = ImageFont.truetype(os.path.join(picdir, '原神cn.ttf'), 13) #字体文件
################################引入配置文件结束################################################
def Local_strong_brush(): #局部强制刷新显示
i = 0
while i < 5:
epd.displayPartial(epd.getbuffer(info_image.rotate(180)))#局刷开始
i = i + 1
def get_date():#返回当前年月日及星期几
date = datetime.datetime.now()
today=LunarDate.today()
week_day_dict = {0: '星期一',1: '星期二',2: '星期三',3: '星期四',4: '星期五',5: '星期六',6: '星期日',}
day = date.weekday()
return time.strftime('%Y年%m月%d日')+''+week_day_dict[day]+''+today.strftime('农历%M月%D')
def get_time():#返回当前时间,不到秒,大写
return time.strftime('%H:%M')
def Get_address():#获取当前的IP地址
return (subprocess.check_output(u"hostname -I | cut -d\' \' -f1 | head --bytes -1", shell = True ).decode('gbk'))
def Get_ipv4_address(): # 获取当前的IP地址
try:
# 执行命令获取IP地址并处理输出以仅返回IPv4地址
ip_output = subprocess.check_output("hostname -I | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}'", shell=True).decode('utf-8').strip()
# 分割输出以获取单个IP地址列表
ip_list = ip_output.split()
# 过滤掉以172开头的IP地址
filtered_ips = [ip for ip in ip_list if not ip.startswith("172.")]
# 如果有有效的IP地址返回第一个否则返回获取失败
if filtered_ips:
return filtered_ips[0]
else:
return "地址获取失败"
except subprocess.CalledProcessError as e:
#logging.error("获取IPv4地址失败: %s", e)
return "获取失败"
def CPU_temperature():#CPU温度获取
temperatura = os.popen('vcgencmd measure_temp').readline()
temperatura = temperatura.replace('temp=','').strip()
return str(temperatura)
def Memory_footprint():#显示内存占用百分比
return(subprocess.check_output(u"free -m | awk -F '[ :]+' 'NR==2{printf \"%d\", ($3)/$2*100}'", shell = True ).decode('gbk'))
def CPU_usage(): #显示CPU占用百分比
return(str(int(float(os.popen("top -b -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))))
def power_battery():#获取当前电池电量
return(str(int(subprocess.check_output(u"echo \"get battery\" | nc -q 0 127.0.0.1 8423|awk -F':' '{print int($2)}'", shell = True ).decode('gbk')))+u'%')
# 打印电量信息
print(power_battery())
def Bottom_edge(): #在图片中添加底边内容
draw.rectangle((0, 105, 250, 122), 'black', 'black')
'''电池图标画图'''
draw.line((126,109,154,109),fill=255, width=1) #电池顶边
draw.line((126,110,126,119),fill=255, width=1) #电池左边
draw.line((127,119,154,119),fill=255, width=1) #电池下边
draw.line((154,110,154,118),fill=255, width=1)
draw.line((155,112,157,112),fill=255, width=1)
draw.line((155,116,157,116),fill=255, width=1)
draw.line((157,113,157,115),fill=255, width=1)
global power_str
power_str=power_battery()
draw.text((129,108),power_str,font = font04,fill =255) #显示当前电量百分比
'''电池图标画图'''
draw.ellipse((192, 107, 207, 120), 0, 255)# 时钟图标
draw.line((199,109,199,114),fill=255, width=1)
draw.line((200,114,204,114),fill=255, width=1)
global local_addr #获取当前IP地址
local_addr= Get_ipv4_address() #获取当前IP地址
draw.text((10,107),"IP:"+local_addr,font = font05,fill =255)#显示当前IP地址
def Weather(): #在图片中添加天气内容
Weather_json = open('/root/2.13-Ink-screen-clock/bin/weather.json','r')
Weather_data = Weather_json.read()
Weather_json.close()
Weather_text=json.loads(Weather_data)
global Weather_position
global temperature
global weather
global wind_direction
global weather_update
global weather_date
global humidity
Weather_position = Weather_text['cityname'] #定位位置
temperature=Weather_text['temp']+u'°C' #温度
weather = Weather_text['weather'] #天气情况
wind_direction = Weather_text['WD'] #风向
weather_update = Weather_text['time'] #天气更新时间
weather_date = Weather_text['date'] #日期
humidity = Weather_text['SD'] #湿度
draw.text((150,25),"天气:",font = font06,fill =0)#显示当前天气前缀
draw.text((150,45),"温度:",font = font06,fill =0)#显示当前温度前缀
draw.text((150,65),"湿度:",font = font06,fill =0)#显示当前湿度前缀
draw.text((150,85),"城市:",font = font06,fill =0)#显示当前城市前缀
draw.text((191,25),weather,font = font06,fill =0)
draw.text((191,45),temperature,font = font06,fill =0)
draw.text((191,65),humidity,font = font06,fill =0)
draw.text((191,85),Weather_position,font = font06,fill =0)
draw.text((211,107),weather_update,font = font05,fill =255) #显示天气更新时间
def Basic_refresh(): #全刷函数
logging.info("在启动画布之前,刷新并准备基本内容")#开始画布前刷新准备基础内容
global get_date_var
get_date_var=get_date() #记录开始数据
draw.text((2,2),get_date_var,font = font02,fill =0)#将日期及星期几显示到屏幕
global local_time
local_time=get_time()
draw.text((5,28),local_time,font = font03,fill =0)#显示当前时间
Bottom_edge() #添加底边内容
Weather() #天气内容
epd.display(epd.getbuffer(info_image.rotate(180)))
def Partial_full_brush(): #局部定时全刷函数
Basic_refresh() #全局刷新
logging.debug("局部定时全局刷新")
epd.init()
def Partial_refresh():#局刷函数
logging.info("部分内容更新,此更新建议与分钟同步,以节省墨水屏的使用寿命")#局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命
epd.displayPartBaseImage(epd.getbuffer(info_image.rotate(180)))
epd.init()
while (True):
global local_time
local_time1=get_time()
if (local_time1==local_time) ==False:
draw.rectangle((5, 28, 149, 82), fill = 255) #时间局刷区域
draw.text((5,28),local_time1,font = font03,fill =0)#刷新当前时间
local_time=local_time1
Local_strong_brush() #局部强刷
get_date_var1=get_date() #局刷判断,如果时间与前一次不一致说明内容变化,需要刷新显示
global get_date_var #再次声明这个是全局变量
if(get_date_var1==get_date_var) ==False:
draw.rectangle((2, 2, 250, 16), fill = 255) #设置头部刷新区域
draw.text((2,2),get_date_var1,font = font02,fill =0)#将日期及星期几刷新显示到屏幕
get_date_var=get_date_var1 #将更新的值保存到初始变量,直到下一次变化时执行该刷新操作
logging.debug("头部日期部位发生刷新变化.")
Local_strong_brush() #局部强刷
global local_addr #当前IP地址
local_addr1 = Get_ipv4_address()
if (local_addr1==local_addr) ==False:
draw.rectangle((1, 107, 123, 120), fill = 0) #设置头部刷新区域
draw.text((10,107),"IP:"+local_addr1,font = font05,fill =255)#显示当前IP地址
local_addr=local_addr1
Local_strong_brush() #局部强刷
'''天气局部更新函数'''
Weather_json = open('/root/2.13-Ink-screen-clock/bin/weather.json','r')
Weather_data = Weather_json.read()
Weather_json.close()
Weather_text=json.loads(Weather_data)
global Weather_position
global temperature
global weather
global wind_direction
global weather_update
global weather_date
global humidity
Weather_position1 = Weather_text['cityname'] #定位位置
temperature1=Weather_text['temp']+u'°C' #温度
weather11 = Weather_text['weather'] #天气情况
wind_direction1 = Weather_text['WD'] #风向
weather_update1 = Weather_text['time'] #天气更新时间
weather_date1 = Weather_text['date'] #日期
humidity1 = Weather_text['SD'] #湿度
if (weather11==weather) ==False:
draw.rectangle((191, 25, 249, 38), fill = 255) #天气局刷区域
draw.text((191,25),weather11,font = font06,fill =0)
weather=weather11
logging.info("天气局部刷新")
Local_strong_brush() #局部强刷
if (temperature1==temperature) ==False:
draw.rectangle((191, 45, 249, 57), fill = 255) #局刷区域
draw.text((191,45),temperature1,font = font06,fill =0)
temperature=temperature1
logging.info("温度局部刷新")
Local_strong_brush() #局部强刷
if (humidity1==humidity) ==False:
draw.rectangle((191, 65, 249, 77), fill = 255) #局刷区域
draw.text((191,65),humidity1,font = font06,fill =0)
humidity = humidity1
logging.info("湿度局部刷新")
Local_strong_brush() #局部强刷
if (Weather_position1==Weather_position) ==False:
draw.rectangle((191, 85, 249, 98), fill = 255) #局刷区域
draw.text((191,85),Weather_position1,font = font06,fill =0)
Weather_position = Weather_position1
logging.info("城市局部刷新")
Local_strong_brush() #局部强刷
if (weather_update1==weather_update) ==False:
draw.rectangle((211, 107, 248, 118), fill = 0) #设置更新时间刷新区域
draw.text((211,107),weather_update1,font = font05,fill =255) #显示天气更新时间
weather_update=weather_update1
logging.info("天气更新时间局部刷新")
Local_strong_brush() #局部强刷
'''天气局部更新函数'''
global power_str
power_str1 =power_battery()
if (power_str1==power_str) ==False:
draw.rectangle((128, 110, 153, 117), fill = 0) #设置更新时间刷新区域
draw.text((129,108),power_battery(),font = font04,fill =255) #显示当前电量百分比
power_str=power_str1
Local_strong_brush() #局部强刷
# logging.info("电池电量局部刷新")
retry_interval = 180 # 设置重试间隔时间(秒)
while True:
try:
##################屏幕初始化#########################
epd = epd2in13_V4.EPD() #初始化
epd.init()#设定屏幕刷新模式
#epd.Clear(0xFF) #清除屏幕内容
##################屏幕初始化#########################
logging.info("Width = %s, Height = %s", format(epd.width), format(epd.height)) #打印屏幕高度及宽度
logging.info("初始化并清空显示屏")#屏幕开始准备相关展示
info_image = Image.new('1', (epd.height, epd.width), 255) #画布创建准备
draw = ImageDraw.Draw(info_image)
Basic_refresh() #全局刷新
Partial_refresh() #局部刷新
epd.init()
epd.Clear(0xFF)
epd.sleep()
time.sleep(300)
break # 如果脚本执行成功,则退出循环
except (OSError, Exception) as e: # 捕获你提到的异常
logging.error("发生了错误: %s", e)
time.sleep(retry_interval) # 等待一段时间后重试
except KeyboardInterrupt:
logging.info("检测到键盘中断,正在清理并退出")
epd.init()
epd.Clear(0xFF) # 清除屏幕内容
epd.sleep() # 使屏幕进入休眠状态
epd2in13_V4.epdconfig.module_exit() # 清理资源
exit()
# 脚本正常结束后的清理操作
epd.init()
epd.Clear(0xFF) # 清除屏幕内容
epd.sleep() # 使屏幕进入休眠状态
epd2in13_V4.epdconfig.module_exit() # 清理资源
exit()

View File

@@ -5,6 +5,9 @@ import json
import time import time
import logging import logging
import requests import requests
import random
import socket
from functools import wraps
from threading import Timer from threading import Timer
logging.basicConfig( logging.basicConfig(
@@ -13,13 +16,162 @@ logging.basicConfig(
datefmt='%Y-%m-%d %H:%M:%S' datefmt='%Y-%m-%d %H:%M:%S'
) )
def check_network_connection():
"""检查网络连接状态"""
try:
# 尝试连接一个可靠的公共DNS服务器
socket.create_connection(("223.5.5.5", 53), timeout=5)
return True
except OSError:
return False
def get_ip():
"""改进的IP获取函数"""
if not check_network_connection():
logging.warning("网络连接不可用")
return None
services = [
{"url": "https://api.ipify.org?format=json", "field": "ip"},
{"url": "https://ipinfo.io/json", "field": "ip"},
{"url": "https://ifconfig.me/all.json", "field": "ip_addr"}
]
for service in services:
try:
resp = requests.get(service["url"], timeout=10)
data = resp.json()
return data.get(service["field"])
except Exception:
continue
logging.error("所有IP服务尝试失败")
return None
def get_ip():
"""从ip.cn获取当前IP地址"""
url = "https://ip.cn/api/index?ip=&type=0"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
try:
resp = requests.get(url, headers=headers, timeout=10)
resp.raise_for_status()
data = resp.json()
if data.get('code') == 'Success':
return data.get('ip', '')
logging.error("获取IP失败: %s", data.get('msg', '未知错误'))
except Exception as e:
logging.error("获取IP异常: %s", str(e))
return None
def get_current_city():
"""通过IP地址获取当前定位城市并去除''后缀"""
ip = get_ip()
if not ip:
return None
url = f"http://ip-api.com/json/{ip}?fields=city&lang=zh-CN"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
while True:
try:
resp = requests.get(url, headers=headers, timeout=10)
data = resp.json()
if data.get('status') == 'success':
return data.get('city', '').replace('', '')
logging.error("定位失败: %s", data.get('message', '未知错误'))
except Exception as e:
logging.error("定位异常: %s", str(e))
time.sleep(180)
def schedule_getWeath():
"""改进的定时任务调度"""
retry_count = 0
max_retries = 3
while retry_count < max_retries:
try:
getWeath()
break
except Exception as e:
retry_count += 1
wait_time = min(300, retry_count * 60) # 指数退避
logging.error(f"天气更新失败({retry_count}/{max_retries}): {str(e)}")
time.sleep(wait_time)
# 无论成功与否,都安排下一次执行
Timer(1800, schedule_getWeath).start() # 30分钟间隔
def getWeath(default_city='101060101'):
"""改进的天气获取函数"""
# 1. 检查网络连接
if not check_network_connection():
logging.error("网络不可用,跳过天气更新")
return
# 2. 获取城市信息
city_name = None
try:
city_name = get_current_city()
except Exception as e:
logging.error(f"获取城市失败: {str(e)}")
# 3. 确定区域ID
area_id = default_city
if city_name:
try:
found_id = get_area_id(city_name)
if found_id:
area_id = found_id
logging.info(f"使用城市区域ID: {area_id} ({city_name})")
except Exception as e:
logging.error(f"获取区域ID失败: {str(e)}")
# 4. 获取天气数据
weather_apis = [
f'https://d1.weather.com.cn/sk_2d/{area_id}.html',
f'https://www.weather.com.cn/weather1d/{area_id}.shtml'
]
for api_url in weather_apis:
try:
resp = requests.get(
api_url,
headers={'User-Agent': 'Mozilla/5.0'},
timeout=15
)
resp.raise_for_status()
# 处理不同API的响应格式
if 'sk_2d' in api_url:
weather_data = resp.content[11:].decode('utf-8')
else:
weather_data = parse_html_weather(resp.text)
# 验证数据有效性
if validate_weather_data(weather_data):
with open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w') as f:
json.dump(weather_data, f)
logging.info("天气数据更新成功")
return
except Exception as e:
logging.warning(f"天气API {api_url} 失败: {str(e)}")
continue
logging.error("所有天气API尝试失败")
def get_area_id(city_name): def get_area_id(city_name):
"""从city.js中检索AREAID无限重试直到成功""" """从city.js中检索AREAID无限重试直到成功"""
url = "https://j.i8tq.com/weather2020/search/city.js" url = "https://j.i8tq.com/weather2020/search/city.js"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
while True: while True:
try: try:
resp = requests.get(url, timeout=10) resp = requests.get(url, headers=headers, timeout=10)
resp.encoding = 'utf-8' # 显式设置编码 resp.encoding = 'utf-8'
city_data = json.loads(resp.text.split('=', 1)[1].rstrip(';')) city_data = json.loads(resp.text.split('=', 1)[1].rstrip(';'))
for province in city_data.values(): for province in city_data.values():
for city in province.values(): for city in province.values():
@@ -31,68 +183,10 @@ def get_area_id(city_name):
logging.error("获取城市ID失败: %s", str(e)) logging.error("获取城市ID失败: %s", str(e))
time.sleep(180) time.sleep(180)
def get_current_city():
"""获取当前定位城市并去除''后缀"""
url = "http://ip-api.com/json/?lang=zh-CN"
while True:
try:
resp = requests.get(url, timeout=10)
resp.encoding = 'utf-8' # 显式设置编码
data = resp.json()
if data['status'] == 'success':
return data['city'].rstrip('')
logging.error("定位失败: %s", data.get('message'))
except Exception as e:
logging.error("定位异常: %s", str(e))
time.sleep(180)
def schedule_getWeath():
"""定时任务调度"""
try:
getWeath()
finally:
Timer(180, schedule_getWeath).start()
def getWeath(default_city='101060101'):
"""获取天气数据核心函数"""
city_name = get_current_city()
area_id = default_city # 设置默认值
if city_name:
try:
area_id = get_area_id(city_name) or default_city
except Exception as e:
logging.error("获取区域ID失败: %s", str(e))
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Referer': 'https://www.weather.com.cn/'
}
try:
resp = requests.get(
f'https://d1.weather.com.cn/sk_2d/{area_id}.html',
headers=headers,
timeout=15
)
resp.encoding = 'utf-8' # 关键修改:强制设置响应编码
resp.raise_for_status()
# 直接处理原始字节数据
weather_data = resp.content[11:].decode('utf-8')
# 使用utf-8编码写入文件
with open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w', encoding='utf-8') as f:
f.write(weather_data)
logging.info("天气数据更新成功")
except Exception as e:
logging.error("天气更新失败: %s", str(e))
if __name__ == "__main__": if __name__ == "__main__":
try: try:
schedule_getWeath() schedule_getWeath()
while True: while True:
time.sleep(1) # 保持主线程存活 time.sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info("程序已终止") logging.info("程序已终止")

BIN
pic/Fonttt.ttf Normal file

Binary file not shown.

BIN
pic/GB2312.ttf Normal file

Binary file not shown.

BIN
pic/SmileySans-Oblique.otf Normal file

Binary file not shown.

Binary file not shown.

BIN
pic/SmileySans-Oblique.ttf Normal file

Binary file not shown.

Binary file not shown.

BIN
pic/原神cn.ttf Normal file

Binary file not shown.

BIN
pic/星穹铁道cn.ttf Normal file

Binary file not shown.