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