Skip to content

Commit

Permalink
correct logger request format and add example config
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwaalsh committed Oct 8, 2024
1 parent d139a61 commit 9e2e876
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
27 changes: 27 additions & 0 deletions configs/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions src/phyto_arm/src/digital_logger_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
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')
outlets = rospy.get_param('/digital_logger/outlets')

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')
Expand All @@ -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():
Expand All @@ -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'
Expand Down

0 comments on commit 9e2e876

Please sign in to comment.