Commit 46d0cba6 刘韬

新增/rest/api/v1/shelf/allConfigPosOn接口,优化现有posOn和posOff接口的日志记录与错误处理,更新版本号至1.73

1 个父辈 68b0632a
此文件的差异被折叠, 点击展开。
...@@ -721,6 +721,38 @@ def rest_api_v1_shelf_allPosOn(): ...@@ -721,6 +721,38 @@ def rest_api_v1_shelf_allPosOn():
logging.error(f'[allPosOn] error: {e}') logging.error(f'[allPosOn] error: {e}')
return 'allPosOn FAIL' return 'allPosOn FAIL'
#/rest/api/v1/shelf/allConfigPosOn
#allConfigPosOn OK 或 allConfigPosOn FAIL
@app.route('/rest/api/v1/shelf/allConfigPosOn', methods=['Get'])
def rest_api_v1_shelf_allConfigPosOn():
"""
仅按库位配置点亮所有库位灯(不会点亮非库位灯)。
"""
client_ip = request.remote_addr
logging.info(f'[allConfigPosOn] start turning on configured position lights, from {client_ip}')
try:
strip1 = get_strip(SET_LED_CHANNEL['1'])
strip2 = get_strip(SET_LED_CHANNEL['2'])
turned_on = 0
for posname, cfg in config_dict.items():
try:
led_index = int(cfg.split('@')[0])
channel = cfg.split('@')[1]
strip = strip1 if channel == '1' else strip2
strip.setPixelColor(led_index, Color(255,255,255))
turned_on += 1
except Exception as one_err:
logging.warning(f'[allConfigPosOn] skip invalid config pos="{posname}", cfg="{cfg}", err={one_err}')
strip1.show()
strip2.show()
logging.info(f'[allConfigPosOn] finished, configured positions on={turned_on}')
return 'allConfigPosOn OK'
except Exception as e:
logging.error(f'[allConfigPosOn] error: {e}')
return 'allConfigPosOn FAIL'
#/rest/api/v1/shelf/allPosOff #/rest/api/v1/shelf/allPosOff
#allPosOff OK allPosOff FAIL #allPosOff OK allPosOff FAIL
@app.route('/rest/api/v1/shelf/allPosOff', methods=['Get']) @app.route('/rest/api/v1/shelf/allPosOff', methods=['Get'])
......
1.72
\ No newline at end of file \ No newline at end of file
1.73
\ No newline at end of file \ No newline at end of file
...@@ -589,83 +589,188 @@ def apiClose(): ...@@ -589,83 +589,188 @@ def apiClose():
#posOn OK 或 posOn FAIL #posOn OK 或 posOn FAIL
@app.route('/rest/api/v1/shelf/posOn', methods=['Get']) @app.route('/rest/api/v1/shelf/posOn', methods=['Get'])
def rest_api_v1_shelf_posOn(): def rest_api_v1_shelf_posOn():
"""
亮灯接口:
- 请求参数: posId=库位条码@颜色;库位条码@颜色
- 返回: 'posOn OK' / 'posOn FAIL'
"""
client_ip = request.remote_addr
posId = request.args.get('posId', '').strip()
logging.info(f'[posOn] request from {client_ip}, raw posId="{posId}"')
if not posId:
logging.warning('[posOn] missing posId parameter')
return 'posOn FAIL'
strip1 = get_strip(SET_LED_CHANNEL['1']) strip1 = get_strip(SET_LED_CHANNEL['1'])
strip2 = get_strip(SET_LED_CHANNEL['2']) strip2 = get_strip(SET_LED_CHANNEL['2'])
posId = request.args.get('posId')
option_list = posId.split(';') option_list = [x for x in posId.split(';') if x]
success = True success = True
for ol in option_list: for ol in option_list:
ds = ol.split('@') try:
posname = ds[0] if '@' not in ol:
color = ds[1] logging.warning(f'[posOn] invalid format (missing @): "{ol}"')
x = config_dict.get(posname) success = False
if x is None: continue
success=False
continue posname, color = ol.split('@', 1)
channel = config_dict.get(posname).split('@')[1] posname = posname.strip()
led_index = int(config_dict.get(posname).split('@')[0]) color = color.strip()
s = strip1
if channel == '2': cfg = config_dict.get(posname)
s = strip2 if cfg is None:
s.setPixelColor(led_index, setcolor(color)) logging.warning(f'[posOn] position not found in config: "{posname}"')
success = False
continue
led_index = int(cfg.split('@')[0])
channel = cfg.split('@')[1]
strip = strip1
if channel == '2':
strip = strip2
strip.setPixelColor(led_index, setcolor(color))
logging.info(f'[posOn] turn on light pos="{posname}", color="{color}", channel={channel}, index={led_index}')
except Exception as e:
success = False
logging.error(f'[posOn] error processing "{ol}": {e}')
strip1.show() strip1.show()
strip2.show() strip2.show()
if success: if success:
logging.info('[posOn] finished successfully')
return 'posOn OK' return 'posOn OK'
else: else:
logging.warning('[posOn] finished with failure')
return 'posOn FAIL' return 'posOn FAIL'
#/rest/api/v1/shelf/posOff?posId=1_3_1;1_3_2;1_3_4 #/rest/api/v1/shelf/posOff?posId=1_3_1;1_3_2;1_3_4
#posOff OK 或 posOff FAIL #posOff OK 或 posOff FAIL
@app.route('/rest/api/v1/shelf/posOff', methods=['Get']) @app.route('/rest/api/v1/shelf/posOff', methods=['Get'])
def rest_api_v1_shelf_posOff(): def rest_api_v1_shelf_posOff():
"""
灭灯接口:
- 请求参数: posId=库位条码;库位条码
- 返回: 'posOff OK' / 'posOff FAIL'
"""
client_ip = request.remote_addr
posId = request.args.get('posId', '').strip()
logging.info(f'[posOff] request from {client_ip}, raw posId="{posId}"')
if not posId:
logging.warning('[posOff] missing posId parameter')
return 'posOff FAIL'
strip1 = get_strip(SET_LED_CHANNEL['1']) strip1 = get_strip(SET_LED_CHANNEL['1'])
strip2 = get_strip(SET_LED_CHANNEL['2']) strip2 = get_strip(SET_LED_CHANNEL['2'])
posId = request.args.get('posId')
option_list = posId.split(';') option_list = [x for x in posId.split(';') if x]
success = True success = True
for posname in option_list: for posname in option_list:
x = config_dict.get(posname) try:
if x is None: posname = posname.strip()
success=False cfg = config_dict.get(posname)
continue if cfg is None:
channel = config_dict.get(posname).split('@')[1] logging.warning(f'[posOff] position not found in config: "{posname}"')
led_index = int(config_dict.get(posname).split('@')[0]) success = False
s = strip1 continue
if channel == '2':
s = strip2 led_index = int(cfg.split('@')[0])
s.setPixelColor(led_index, Color(0,0,0)) channel = cfg.split('@')[1]
strip = strip1
if channel == '2':
strip = strip2
strip.setPixelColor(led_index, Color(0,0,0))
logging.info(f'[posOff] turn off light pos="{posname}", channel={channel}, index={led_index}')
except Exception as e:
success = False
logging.error(f'[posOff] error processing "{posname}": {e}')
strip1.show() strip1.show()
strip2.show() strip2.show()
if success: if success:
logging.info('[posOff] finished successfully')
return 'posOff OK' return 'posOff OK'
else: else:
logging.warning('[posOff] finished with failure')
return 'posOff FAIL' return 'posOff FAIL'
#/rest/api/v1/shelf/allPosOn #/rest/api/v1/shelf/allPosOn
#allPosOn OK 或 allPosOn FAIL #allPosOn OK 或 allPosOn FAIL
@app.route('/rest/api/v1/shelf/allPosOn', methods=['Get']) @app.route('/rest/api/v1/shelf/allPosOn', methods=['Get'])
def rest_api_v1_shelf_allPosOn(): def rest_api_v1_shelf_allPosOn():
logging.info('开始打开所有灯') client_ip = request.remote_addr
for pin in [SET_LED_CHANNEL['1'],SET_LED_CHANNEL['2']]: logging.info(f'[allPosOn] start turning on all lights, from {client_ip}')
strip = get_strip(pin) try:
for i in range(0,strip.numPixels()): for pin in [SET_LED_CHANNEL['1'], SET_LED_CHANNEL['2']]:
strip.setPixelColor(i, Color(255,255,255)) strip = get_strip(pin)
strip.show() for i in range(0, strip.numPixels()):
logging.info('完成打开所有灯') strip.setPixelColor(i, Color(255,255,255))
return 'allPosOn OK' strip.show()
logging.info('[allPosOn] finished turning on all lights')
return 'allPosOn OK'
except Exception as e:
logging.error(f'[allPosOn] error: {e}')
return 'allPosOn FAIL'
#/rest/api/v1/shelf/allConfigPosOn
#allConfigPosOn OK 或 allConfigPosOn FAIL
@app.route('/rest/api/v1/shelf/allConfigPosOn', methods=['Get'])
def rest_api_v1_shelf_allConfigPosOn():
"""
仅按库位配置点亮所有库位灯(不会点亮非库位灯)。
"""
client_ip = request.remote_addr
logging.info(f'[allConfigPosOn] start turning on configured position lights, from {client_ip}')
try:
strip1 = get_strip(SET_LED_CHANNEL['1'])
strip2 = get_strip(SET_LED_CHANNEL['2'])
turned_on = 0
for posname, cfg in config_dict.items():
try:
led_index = int(cfg.split('@')[0])
channel = cfg.split('@')[1]
strip = strip1 if channel == '1' else strip2
strip.setPixelColor(led_index, Color(255,255,255))
turned_on += 1
except Exception as one_err:
logging.warning(f'[allConfigPosOn] skip invalid config pos="{posname}", cfg="{cfg}", err={one_err}')
strip1.show()
strip2.show()
logging.info(f'[allConfigPosOn] finished, configured positions on={turned_on}')
return 'allConfigPosOn OK'
except Exception as e:
logging.error(f'[allConfigPosOn] error: {e}')
return 'allConfigPosOn FAIL'
#/rest/api/v1/shelf/allPosOff #/rest/api/v1/shelf/allPosOff
#allPosOff OK allPosOff FAIL #allPosOff OK allPosOff FAIL
@app.route('/rest/api/v1/shelf/allPosOff', methods=['Get']) @app.route('/rest/api/v1/shelf/allPosOff', methods=['Get'])
def rest_api_v1_shelf_allPosOff(): def rest_api_v1_shelf_allPosOff():
logging.info('开始关闭所有灯') client_ip = request.remote_addr
for pin in [SET_LED_CHANNEL['1'],SET_LED_CHANNEL['2']]: logging.info(f'[allPosOff] start turning off all lights, from {client_ip}')
strip = get_strip(pin) try:
for i in range(0,strip.numPixels()): for pin in [SET_LED_CHANNEL['1'], SET_LED_CHANNEL['2']]:
strip.setPixelColor(i, Color(0,0,0)) strip = get_strip(pin)
strip.show() for i in range(0, strip.numPixels()):
logging.info('完成关闭所有灯') strip.setPixelColor(i, Color(0,0,0))
return 'allPosOff OK' strip.show()
logging.info('[allPosOff] finished turning off all lights')
return 'allPosOff OK'
except Exception as e:
logging.error(f'[allPosOff] error: {e}')
return 'allPosOff FAIL'
@app.route('/rest/api/v1/shelf/keepAlive') @app.route('/rest/api/v1/shelf/keepAlive')
def rest_api_v1_shelf_keepAlive(): def rest_api_v1_shelf_keepAlive():
return 'OK' return 'OK'
......
1.72
\ No newline at end of file \ No newline at end of file
1.73
\ No newline at end of file \ No newline at end of file
此文件太大,无法显示。
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!