new file: DGConfig.py
modified: DoGeasLAB.py
This commit is contained in:
parent
974e5b08d8
commit
0cbc53788c
5
DGConfig.py
Normal file
5
DGConfig.py
Normal file
@ -0,0 +1,5 @@
|
||||
interval = 0.1
|
||||
wakeSignalStrength=20
|
||||
defalutStrength=10
|
||||
max_retries = 3 # 最大重试次数
|
||||
retry_delay = 1 # 重试间隔时间(秒)
|
29
DoGeasLAB.py
29
DoGeasLAB.py
@ -5,9 +5,9 @@ import logging
|
||||
import pydglab
|
||||
from pydglab import model_v3
|
||||
import requests
|
||||
import DGConfig as cfg
|
||||
|
||||
#参数区
|
||||
interval = 0.1
|
||||
|
||||
#变量区
|
||||
currentStA=0
|
||||
@ -16,14 +16,13 @@ calculatedStA=0
|
||||
calculatedStB=0
|
||||
dglab_instance=pydglab.dglab_v3()
|
||||
data={}
|
||||
dataGot=False
|
||||
|
||||
#连接主机
|
||||
async def connect():
|
||||
retry_count = 0
|
||||
max_retries = 3 # 最大重试次数
|
||||
retry_delay = 1 # 重试间隔时间(秒)
|
||||
await pydglab.scan()
|
||||
while retry_count < max_retries:
|
||||
while retry_count < cfg.max_retries:
|
||||
try:
|
||||
print("正在连接...")
|
||||
await dglab_instance.create()
|
||||
@ -31,18 +30,19 @@ async def connect():
|
||||
return True # 连接成功
|
||||
except Exception as e:
|
||||
retry_count += 1
|
||||
print(f"连接失败(尝试 {retry_count}/{max_retries}):{e}")
|
||||
if retry_count < max_retries:
|
||||
print(f"{retry_delay} 秒后重试...")
|
||||
await asyncio.sleep(retry_delay)
|
||||
print(f"连接失败(尝试 {retry_count}/{cfg.max_retries}):{e}")
|
||||
if retry_count < cfg.max_retries:
|
||||
print(f"{cfg.retry_delay} 秒后重试...")
|
||||
await asyncio.sleep(cfg.retry_delay)
|
||||
else:
|
||||
logging.error("已达到最大重试次数,连接失败。")
|
||||
return False # 连接失败
|
||||
|
||||
#抓取数据
|
||||
async def getData():
|
||||
global data
|
||||
global data,dataGot
|
||||
indicators_url = "http://localhost:8111/indicators"
|
||||
dataGot=True
|
||||
#获取数据
|
||||
try:
|
||||
response=requests.get(indicators_url)
|
||||
@ -51,14 +51,15 @@ async def getData():
|
||||
#获取失败
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"获取数据失败 :{e}")
|
||||
dataGot=False
|
||||
return
|
||||
except ValueError as e:
|
||||
print(f"获取数据失败 : {e}")
|
||||
dataGot=False
|
||||
return
|
||||
|
||||
#强度计算
|
||||
async def setStrength():
|
||||
global interval
|
||||
#计算
|
||||
global data,currentStA,currentStB,calculatedStA,calculatedStB
|
||||
calculatedStA=data.get("crew_total")*4
|
||||
@ -68,8 +69,6 @@ async def setStrength():
|
||||
|
||||
#主逻辑
|
||||
async def main():
|
||||
wakeSignalStrength=20
|
||||
defalutStrength=10
|
||||
print("启动,准备连接")
|
||||
if not await connect():
|
||||
return # 连接失败,退出函数
|
||||
@ -79,9 +78,9 @@ async def main():
|
||||
model_v3.Wave_set["Going_Faster"], model_v3.ChannelA
|
||||
)
|
||||
#连接成功,通电提醒
|
||||
await dglab_instance.set_strength_sync(wakeSignalStrength, wakeSignalStrength)
|
||||
await dglab_instance.set_strength_sync(cfg.wakeSignalStrength, cfg.wakeSignalStrength)
|
||||
await asyncio.sleep(1)
|
||||
await dglab_instance.set_strength_sync(defalutStrength,defalutStrength)
|
||||
await dglab_instance.set_strength_sync(cfg.defalutStrength,cfg.defalutStrength)
|
||||
#循环逻辑
|
||||
while True:
|
||||
await getData()
|
||||
@ -91,7 +90,7 @@ async def main():
|
||||
if(calculatedStA!=currentStA or calculatedStB!=currentStB):
|
||||
print(f"强度修改为:{calculatedStA},{calculatedStB}")
|
||||
await dglab_instance.set_strength_sync(calculatedStA, calculatedStB)
|
||||
await asyncio.sleep(interval)
|
||||
await asyncio.sleep(cfg.interval)
|
||||
return
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user