Wrap generated driver code in 'catch' and handle_exception blocks

This commit is contained in:
Douglas Clowes
2014-03-18 12:42:56 +11:00
parent 7269776484
commit 94e2e06f22
3 changed files with 408 additions and 333 deletions

View File

@@ -7,67 +7,79 @@ namespace eval ::scobj::tank {
}
proc ::scobj::tank::debug_log {debug_level debug_string} {
if {${debug_level} >= ${::scobj::tank::debug_threshold}} {
set fd [open "/tmp/tank.log" "a"]
set line "[clock format [clock seconds] -format "%T"] ${debug_string}"
puts ${fd} "${line}"
close ${fd}
}
set catch_status [ catch {
if {${debug_level} >= ${::scobj::tank::debug_threshold}} {
set fd [open "/tmp/tank.log" "a"]
set line "[clock format [clock seconds] -format "%T"] ${debug_string}"
puts ${fd} "${line}"
close ${fd}
}
} catch_message ]
}
# check function for hset change
proc ::scobj::tank::checkrange {tc_root} {
debug_log 1 "checkrange tc_root=${tc_root} sct=[sct] target=[sct target]"
set setpoint [sct target]
if { [hpropexists [sct] lowerlimit] } {
set lolimit [sct lowerlimit]
} else {
# lowerlimit not set, use target
set lolimit [sct target]
}
if { [hpropexists [sct] upperlimit] } {
set hilimit [sct upperlimit]
} else {
# upperlimit not set, use target
set hilimit [sct target]
}
if { ${setpoint} < ${lolimit} || ${setpoint} > ${hilimit} } {
error "setpoint ${setpoint} violates limits (${lolimit}..${hilimit}) on [sct]"
}
return OK
set catch_status [ catch {
debug_log 1 "checkrange tc_root=${tc_root} sct=[sct] target=[sct target]"
set setpoint [sct target]
if { [hpropexists [sct] lowerlimit] } {
set lolimit [sct lowerlimit]
} else {
# lowerlimit not set, use target
set lolimit [sct target]
}
if { [hpropexists [sct] upperlimit] } {
set hilimit [sct upperlimit]
} else {
# upperlimit not set, use target
set hilimit [sct target]
}
if { ${setpoint} < ${lolimit} || ${setpoint} > ${hilimit} } {
error "setpoint ${setpoint} violates limits (${lolimit}..${hilimit}) on [sct]"
}
return OK
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
# function to request the read of a parameter on a device
proc ::scobj::tank::getValue {tc_root nextState cmd_str} {
debug_log 1 "getValue tc_root=${tc_root} sct=[sct] cmd=${cmd_str}"
if { [hpropexists [sct] geterror] } {
hdelprop [sct] geterror
}
set cmd "${cmd_str}"
debug_log 1 "getValue sct send ${cmd}"
sct send "${cmd}"
return ${nextState}
set catch_status [ catch {
debug_log 1 "getValue tc_root=${tc_root} sct=[sct] cmd=${cmd_str}"
if { [hpropexists [sct] geterror] } {
hdelprop [sct] geterror
}
set cmd "${cmd_str}"
debug_log 1 "getValue sct send ${cmd}"
sct send "${cmd}"
return ${nextState}
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
# function to check the write parameter on a device
proc ::scobj::tank::noResponse {tc_root} {
debug_log 1 "noResponse tc_root=${tc_root} sct=[sct] resp=[sct result]"
return "idle"
set catch_status [ catch {
debug_log 1 "noResponse tc_root=${tc_root} sct=[sct] resp=[sct result]"
return "idle"
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
# function to parse the read of a parameter on a device
proc ::scobj::tank::read_switch {tc_root} {
debug_log 1 "read_switch tc_root=${tc_root} sct=[sct] result=[sct result]"
if { [hpropexists [sct] geterror] } {
hdelprop [sct] geterror
}
set data [sct result]
set nextState "idle"
if {[string equal -nocase -length 7 ${data} "ASCERR:"]} {
# the protocol driver has reported an error
sct geterror "${data}"
return -code error "[sct geterror]"
}
set catch_status [ catch {
debug_log 1 "read_switch tc_root=${tc_root} sct=[sct] result=[sct result]"
if { [hpropexists [sct] geterror] } {
hdelprop [sct] geterror
}
set data [sct result]
set nextState "idle"
if {[string equal -nocase -length 7 ${data} "ASCERR:"]} {
# the protocol driver has reported an error
sct geterror "${data}"
error "[sct geterror]"
}
# hook code starts
if { [string equal -nocase -length 1 "${data}" "?"] } {
sct geterror "Galil error in: '${data}'"
@@ -88,37 +100,42 @@ proc ::scobj::tank::read_switch {tc_root} {
}
}
# hook code ends
if { [hpropexists [sct] geterror] } {
debug_log 1 "[sct] error: [sct geterror]"
return -code error "[sct geterror]"
}
if { ${data} != [sct oldval] } {
debug_log 1 "[sct] changed to new:${data}, from old:[sct oldval]"
sct oldval ${data}
sct update ${data}
sct utime readtime
if { [hpropexists [sct] geterror] } {
debug_log 1 "[sct] error: [sct geterror]"
error "[sct geterror]"
}
return ${nextState}
if { ${data} != [sct oldval] } {
debug_log 1 "[sct] changed to new:${data}, from old:[sct oldval]"
sct oldval ${data}
sct update ${data}
sct utime readtime
}
return ${nextState}
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
# function to write a parameter value on a device
proc ::scobj::tank::setValue {tc_root nextState cmd_str} {
debug_log 1 "setValue tc_root=${tc_root} sct=[sct] cmd=${cmd_str}"
if { [hpropexists [sct] geterror] } {
hdelprop [sct] geterror
}
set par [sct target]
set cmd "${cmd_str}${par}"
if { [hpropexists [sct] driving] } {
if { [hpropexists [sct] writestatus] && [sct writestatus] == "start" } {
sct driving 1
set catch_status [ catch {
debug_log 1 "setValue tc_root=${tc_root} sct=[sct] cmd=${cmd_str}"
if { [hpropexists [sct] geterror] } {
hdelprop [sct] geterror
}
}
debug_log 1 "setValue sct send ${cmd}"
if {![string equal -nocase -length 10 ${cmd} "@@NOSEND@@"]} {
sct send "${cmd}"
}
return ${nextState}
set par [sct target]
set cmd "${cmd_str}${par}"
if { [hpropexists [sct] driving] } {
if { [hpropexists [sct] writestatus] && [sct writestatus] == "start" } {
sct driving 1
}
}
debug_log 1 "setValue sct send ${cmd}"
if {![string equal -nocase -length 10 ${cmd} "@@NOSEND@@"]} {
sct send "${cmd}"
}
return ${nextState}
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
proc ::scobj::tank::mk_sct_tank { sct_controller name } {
@@ -186,7 +203,7 @@ proc ::scobj::tank::mk_sct_tank { sct_controller name } {
# hook code starts
# hook code ends
} catch_message ]
handle_exception ${catch_status} ${catch_message} "in ${ns}::mk_sct_tank"
handle_exception ${catch_status} ${catch_message}
}
namespace eval ::scobj::tank {
@@ -195,20 +212,23 @@ namespace eval ::scobj::tank {
}
proc add_tank {name IP port} {
set ns "::scobj::tank"
${ns}::debug_log 1 "add_tank ${name} ${IP} ${port}"
if {[SplitReply [motor_simulation]]=="false"} {
if {[string equal -nocase "aqadapter" "${IP}"]} {
${ns}::debug_log 1 "makesctcontroller sct_${name} aqadapter ${port}"
makesctcontroller sct_${name} aqadapter ${port}
} else {
${ns}::debug_log 1 "makesctcontroller sct_${name} dmc2280 ${IP}:${port}"
makesctcontroller sct_${name} dmc2280 ${IP}:${port}
set catch_status [ catch {
set ns "::scobj::tank"
${ns}::debug_log 1 "add_tank ${name} ${IP} ${port}"
if {[SplitReply [motor_simulation]]=="false"} {
if {[string equal -nocase "aqadapter" "${IP}"]} {
${ns}::debug_log 1 "makesctcontroller sct_${name} aqadapter ${port}"
makesctcontroller sct_${name} aqadapter ${port}
} else {
${ns}::debug_log 1 "makesctcontroller sct_${name} dmc2280 ${IP}:${port}"
makesctcontroller sct_${name} dmc2280 ${IP}:${port}
}
}
}
${ns}::debug_log 1 "mk_sct_tank sct_${name} ${name}"
${ns}::mk_sct_tank sct_${name} ${name}
close ${fd}
${ns}::debug_log 1 "mk_sct_tank sct_${name} ${name}"
${ns}::mk_sct_tank sct_${name} ${name}
close ${fd}
} catch_message ]
handle_exception ${catch_status} ${catch_message}
}
puts stdout "file evaluation of sct_tank.tcl"