@@ -11,10 +11,22 @@ import PropTypes from 'prop-types'
11
11
import { View } from 'react-native'
12
12
13
13
// Local imports
14
- import { useROS , createROSService , createROSServiceRequest , getValueFromParameter , getParameterFromValue } from '../../ros/ros_helpers'
14
+ import {
15
+ useROS ,
16
+ createROSService ,
17
+ createROSServiceRequest ,
18
+ getValueFromParameter ,
19
+ getParameterFromValue ,
20
+ callROSAction ,
21
+ createROSActionClient ,
22
+ createROSMessage ,
23
+ destroyActionClient
24
+ } from '../../ros/ros_helpers'
15
25
import {
16
26
GET_PARAMETERS_SERVICE_NAME ,
17
27
GET_PARAMETERS_SERVICE_TYPE ,
28
+ RECOMPUTE_WORKSPACE_WALLS_ACTION_NAME ,
29
+ RECOMPUTE_WORKSPACE_WALLS_ACTION_TYPE ,
18
30
SET_PARAMETERS_SERVICE_NAME ,
19
31
SET_PARAMETERS_SERVICE_TYPE
20
32
} from '../Constants'
@@ -30,6 +42,7 @@ const SettingsPageParent = (props) => {
30
42
31
43
// Get relevant local state variables
32
44
const isResettingToPreset = useRef ( false )
45
+ const numTimesResetWorkspaceWalls = useRef ( 0 )
33
46
34
47
// Flag to check if the current orientation is portrait
35
48
const isPortrait = useMediaQuery ( { query : '(orientation: portrait)' } )
@@ -49,6 +62,14 @@ const SettingsPageParent = (props) => {
49
62
let getParametersService = useRef ( createROSService ( ros . current , GET_PARAMETERS_SERVICE_NAME , GET_PARAMETERS_SERVICE_TYPE ) )
50
63
let setParametersService = useRef ( createROSService ( ros . current , SET_PARAMETERS_SERVICE_NAME , SET_PARAMETERS_SERVICE_TYPE ) )
51
64
65
+ /**
66
+ * Create the ROS Action Client. This is created in useRef to avoid
67
+ * re-creating it upon re-renders.
68
+ */
69
+ let recomputeWorkspaceWallsAction = useRef (
70
+ createROSActionClient ( ros . current , RECOMPUTE_WORKSPACE_WALLS_ACTION_NAME , RECOMPUTE_WORKSPACE_WALLS_ACTION_TYPE )
71
+ )
72
+
52
73
/**
53
74
* Get the paramater values in the specified namespace or, if they don't exist,
54
75
* in the default namespace. Set the local state to these values.
@@ -130,20 +151,50 @@ const SettingsPageParent = (props) => {
130
151
console . log ( 'Sending SetParameter request' , currentRequest )
131
152
service . callService ( currentRequest , ( response ) => {
132
153
console . log ( 'For request' , currentRequest , 'received SetParameter response' , response )
133
- // If this is wrapping up a reset to preset, call the callback
134
- if ( isResettingToPreset . current ) {
135
- let resetToPresetSuccessCallback = props . resetToPresetSuccessCallback . current
136
- resetToPresetSuccessCallback ( )
137
- isResettingToPreset . current = false
154
+ // If it was succesful, and if the props specify, then call the action to update the
155
+ // workspace walls.
156
+ if ( response . result . successful ) {
157
+ if ( props . resetWorkspaceWallsOnParameterUpdate ) {
158
+ console . log ( 'Calling recompute_workspace_walls action' )
159
+ // numTimesResetWorkspaceWalls is used to ensure the result callback
160
+ // only gets registered the first time. This is okay because none of the
161
+ // values in the callback are expected to change.
162
+ callROSAction (
163
+ recomputeWorkspaceWallsAction . current ,
164
+ createROSMessage ( { } ) ,
165
+ null ,
166
+ numTimesResetWorkspaceWalls . current > 0
167
+ ? null
168
+ : ( result ) => {
169
+ console . log ( 'Received result from recompute_workspace_walls action' , result )
170
+ // If this is wrapping up a reset to preset, call the callback
171
+ if ( isResettingToPreset . current ) {
172
+ let resetToPresetSuccessCallback = props . resetToPresetSuccessCallback . current
173
+ resetToPresetSuccessCallback ( )
174
+ isResettingToPreset . current = false
175
+ }
176
+ }
177
+ )
178
+ numTimesResetWorkspaceWalls . current ++
179
+ } else {
180
+ // If this is wrapping up a reset to preset, call the callback
181
+ if ( isResettingToPreset . current ) {
182
+ let resetToPresetSuccessCallback = props . resetToPresetSuccessCallback . current
183
+ resetToPresetSuccessCallback ( )
184
+ isResettingToPreset . current = false
185
+ }
186
+ }
138
187
}
139
188
} )
140
189
} , [
141
- props . paramNames ,
190
+ isResettingToPreset ,
191
+ numTimesResetWorkspaceWalls ,
142
192
props . localParamValues ,
143
- settingsPresets ,
193
+ props . paramNames ,
194
+ props . resetToPresetSuccessCallback ,
195
+ props . resetWorkspaceWallsOnParameterUpdate ,
144
196
setParametersService ,
145
- isResettingToPreset ,
146
- props . resetToPresetSuccessCallback
197
+ settingsPresets
147
198
] )
148
199
149
200
/**
@@ -155,6 +206,16 @@ const SettingsPageParent = (props) => {
155
206
setGlobalParameter ( )
156
207
} , [ setGlobalParameter ] )
157
208
209
+ /**
210
+ * When the component is unmounted, destroy the action client.
211
+ */
212
+ useEffect ( ( ) => {
213
+ let action = recomputeWorkspaceWallsAction . current
214
+ return ( ) => {
215
+ destroyActionClient ( action )
216
+ }
217
+ } , [ ] )
218
+
158
219
/**
159
220
* A callback for when the user asks to reset parameters to a preset.
160
221
*/
@@ -294,6 +355,9 @@ SettingsPageParent.propTypes = {
294
355
resetToPresetSuccessCallback : PropTypes . shape ( {
295
356
current : PropTypes . func . isRequired
296
357
} ) ,
358
+ // A prop to specify whether to invoke the action that resets workspace walls
359
+ // when the global parameters have been updated
360
+ resetWorkspaceWallsOnParameterUpdate : PropTypes . bool ,
297
361
// A function to call when the user is done with the page
298
362
doneCallback : PropTypes . func . isRequired
299
363
}
@@ -303,7 +367,8 @@ SettingsPageParent.defaultProps = {
303
367
modalChildren : < > </ > ,
304
368
resetToPresetSuccessCallback : {
305
369
current : ( ) => { }
306
- }
370
+ } ,
371
+ resetWorkspaceWallsOnParameterUpdate : false
307
372
}
308
373
309
374
export default SettingsPageParent
0 commit comments