################################################################################ # # # calnexRest.tcl # # # # TCL interface to the new line of Calnex products # # # # Copyright (c) Calnex Solutions Ltd 2008 - 2018 # # # # The contents of this file should not be copied or distributed # # without permission being granted by Calnex Solutions Ltd. # # # # All rights reserved. # # # ################################################################################ package require rest package require json package require json::write set [namespace current]::__IpAddress "" set [namespace current]::__lastError "" proc __chkForError { command } { global [namespace current]::__lastError if {[string compare $__lastError ""] != 0 } { error "$__lastError (command=\"$command\")" } } proc calnexInit {IpAddr} { global [namespace current]::__IpAddress global [namespace current]::__lastError set __lastErr "" if {$IpAddr == ""} { set __lastError "Must specify an IP Address for the instrument" } else { set __IpAddress $IpAddr set model [calnexGetVal instrument/information HwType] set sn [calnexGetVal instrument/information SerialNumber] puts "$model $sn" } __chkForError "calnexInit" } proc calnexSendReq {req url body} { global [namespace current]::__IpAddress global [namespace current]::__lastError set httpErrorDict [dict create 400 "Bad Request" 404 "Not Found" 500 "Internal Server Error" 501 "Not Implemented"] set ret "" set __lastError "" if {$__IpAddress == ""} { set __lastError "IP address not configured - call calnexInit before any other calls" set ret "" } else { set config "method ${req} headers \{Content-Type application/json\}" #set ret [rest::simple "http://$__IpAddress/api/${url}?format=json" "" $config $body] if { [catch { set ret [rest::simple "http://$__IpAddress/api/${url}?format=json" "" $config $body] } errmsg ] } { set httpErrorString "" set httpStr [lindex [split $errmsg] 0] if {$httpStr == "HTTP"} { set httpErrorNum [lindex [split $errmsg] 1] puts $errmsg puts $httpErrorNum if {[dict exists $httpErrorDict $httpErrorNum]} { set httpErrorString [dict get $httpErrorDict $httpErrorNum] } #throw $httpErrorNum "HTTP $httpErrorNum: $httpErrorString ($url)" set __lastError "HTTP $httpErrorNum: $httpErrorString" } else { set __lastError "$errmsg" } } } return $ret } proc calnexGet {url args} { set body [json::write object {*}$args] set res [json::json2dict [calnexSendReq get $url $body]] __chkForError "calnexGet $url" return $res } proc calnexGetVal {url arg} { global [namespace current]::__lastError set body "" set ret "" set calnexSendReqRet [calnexSendReq get $url $body] __chkForError "calnexGetVal $url" set res [json::json2dict $calnexSendReqRet] if [dict exists $res $arg] { set ret [dict get $res $arg] } else { set __lastError "$arg does not exist in response: $res" } __chkForError "calnexGetVal $url" return $ret } proc calnexSet {url args} { set body [json::write object {*}$args] calnexSendReq put $url $body __chkForError "calnexSet $url" } proc calnexCreate {url args} { set body [json::write object {*}$args] calnexSendReq post $url $body __chkForError "calnexCreate $url" } proc calnexDel {url} { calnexSendReq delete $url "" __chkForError "calnexDel $url" } # # Old syntax - kept for backwards compatibility # proc p100req {req url body} { return [calnexSendReq $req $url $body] } proc p100get {url args} { return [calnexGet $url {*}$args] } proc p100set {url args} { calnexSet $url {*}$args } proc p100create {url args} { calnexCreate $url {*}$args } proc p100del {url} { calnexDel $url } proc a100req {req url body} { return [calnexSendReq $req $url $body] } proc a100get {url args} { return [calnexGet $url {*}$args] } proc a100set {url args} { calnexSet $url {*}$args } proc a100create {url args} { calnexCreate $url {*}$args } proc a100del {url} { calnexDel $url }