From 9e2e8764659b9d589d1244b68023205cbfc75128 Mon Sep 17 00:00:00 2001 From: John Walsh Date: Tue, 8 Oct 2024 17:59:12 -0400 Subject: [PATCH] correct logger request format and add example config --- configs/example.yaml | 27 ++++++++++++++++++++++++ src/phyto_arm/src/digital_logger_node.py | 12 +++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/configs/example.yaml b/configs/example.yaml index 989e965..0518e52 100644 --- a/configs/example.yaml +++ b/configs/example.yaml @@ -253,3 +253,30 @@ web: default: -999.999 # Replace with desired default long defaultOnlyExample: #optional default: 'static_value' # No topic, making this default value permanent + +# Configure digital logger control +# Status topic: /digital_logger/outlets/{outlet num}/status +# D/L message: {"name": "camera", "status": "on"} +# Control service: /digital_logger/control +# C/L message: {"name": "camera", "status": "off"} +digital_logger: + username: "admin" + password: "1234" + address: "192.168.1.5" + outlets: + - name: "ifcb" + outlet: 1 + - name: "arm_ifcb" + outlet: 2 + - name: "arm_chanos" + outlet: 3 + - name: "camera" + outlet: 4 + - name: "gps" + outlet: 5 + - name: "ctd" + outlet: 6 + - name: "winch" + outlet: 7 + - name: "starlink" + outlet: 8 \ No newline at end of file diff --git a/src/phyto_arm/src/digital_logger_node.py b/src/phyto_arm/src/digital_logger_node.py index ba7ab42..2aad98a 100755 --- a/src/phyto_arm/src/digital_logger_node.py +++ b/src/phyto_arm/src/digital_logger_node.py @@ -7,6 +7,9 @@ from phyto_arm.msg import OutletStatus def control_outlet(msg): + """ + Send the given msg to the digital logger as an HTTP request. + """ username = rospy.get_param('/digital_logger/username') password = rospy.get_param('/digital_logger/password') address = rospy.get_param('/digital_logger/address') @@ -14,17 +17,19 @@ def control_outlet(msg): for outlet in outlets: if outlet['name'] == msg.name: + # outlet numbers in the config yaml start from 1 whereas outlet numbers in the API start from 0 outlet_num = int(outlet['outlet']) - 1 status = msg.status == 'on' - response = requests.put(f'http://{address}/restapi/relay/outlets/{outlet_num}/state/', auth=HTTPDigestAuth(username, password), data={'value': status}, headers={"X-CSRF": "x", "Accept": "application/json"}) - rospy.logwarn(f'sent: http://{address}/restapi/relay/outlets/{outlet_num}/state/, {status}, {response.text}') + response = requests.put(f'http://{address}/restapi/relay/outlets/{outlet_num}/state/', auth=HTTPDigestAuth(username, password), data={'value': str(status).lower()}, headers={"X-CSRF": "x", "Accept": "application/json"}) + rospy.loginfo(f'sent: http://{address}/restapi/relay/outlets/{outlet_num}/state/, auth={username},{password} status={str(status).lower()}, received: code {response.status_code} : {response.text}') def main(): rospy.init_node('digital_logger_node') + # subscribe to the digital logger control topic subscriber = rospy.Subscriber('/digital_logger/control', OutletStatus, control_outlet) username = rospy.get_param('/digital_logger/username') @@ -33,10 +38,12 @@ def main(): outlets = rospy.get_param('/digital_logger/outlets') num_outlets = len(outlets) + # create an independent publisher for each outlet outlet_publishers = {} for outlet_num in range(num_outlets): outlet_publishers[f'outlet_{outlet_num}'] = rospy.Publisher(f'/digital_logger/outlet/{outlet_num}/status/', OutletStatus, queue_size=10) + # Monitor outlets at 1Hz rate = rospy.Rate(1) while not rospy.is_shutdown(): @@ -46,6 +53,7 @@ def main(): assert len(result) == num_outlets + # publish the status of each outlet to its specific topic for outlet_index in range(len(result)): if result[outlet_index]: status = 'on'