Posted to tcl by Napier at Sun Jan 04 22:40:31 GMT 2015view raw

  1. proc onData {sockID ip} {
  2. if {[chan gets $sockID line] >= 0} {
  3. LOG "Received Data from $ip ($sockID)"
  4. LOG "$line"
  5. switch -regexp -- $line {
  6. "CONFIRMED" {
  7. # Logged In and Authorized
  8. if {[dict exists $::miDoor::Sockets $sockID]} {
  9. dict set ::miDoor::Devices $ip connecting 0
  10. UCL 1 "miDoor at $ip is Logged in and Ready for Commands"
  11. }
  12. }
  13. "STATUS 0:" {
  14. set r [string trim [string map {"STATUS 0:" ""} $line]]
  15. switch -regexp -- $r {
  16. "ERROR" {
  17. if {[lindex $r 1] == 1003} {
  18. # No More Log Entries are Available
  19. UCL 1 "Done Retrieving miDoor Event Logs"
  20. } else {
  21. UCL 1 "ERROR Detected: [lrange $r 2 end]"
  22. }
  23. }
  24. "ACCEPTED" {
  25. UCL 1 "miDoor has Accepted the Command"
  26. }
  27. }
  28. }
  29. "STATE 0:" {
  30. # Response to a Query
  31. set r [string trim [string map {"STATE 0:" ""} $line]]
  32. set tempDict \
  33. [dict create \
  34. state [lindex $r 0] \
  35. lastChanged [lindex $r 2] \
  36. text [lindex $r 3] \
  37. temp [lindex $r 4] \
  38. alarm [lindex $r 5] \
  39. switch [lindex $r 7] \
  40. admin [lindex $r 8] \
  41. lastResponse [clock milliseconds] \
  42. ]
  43. set currentState [dict get $tempDict state]
  44. if {[dict exists $::miDoor::Devices $ip status state]} {
  45. set oldState [dict get $::miDoor::Devices $ip status state]
  46. } else {
  47. set oldState -1
  48. }
  49. dict set ::miDoor::Devices "$ip" status $tempDict
  50. if {[dict exists $::miDoor::Names $ip]} {
  51. set name [dict get $::miDoor::Names $ip]
  52. } else {
  53. LOG "Name of Device Not Found, Registration Error?"
  54. return
  55. }
  56. if {$oldState == -1} {
  57. LOG "STATE CHANGE STATUS CURRENTLY UNKNOWN"
  58. ::Access::Devices::Update -name "$name" -status [dict get $tempDict state]
  59. return
  60. }
  61. set deviceNumber [::Access::getDeviceNumberbyName $name]
  62. if {[dict exists $::Access::Devices::Listing $deviceNumber status]} {
  63. set shownState [dict get $::Access::Devices::Listing $deviceNumber status]
  64. if {$shownState != $currentState} {
  65. ::Access::Devices::Update -name "$name" -status "$currentState"
  66. ::DeviceEvents::Evaluate 1 -name "$name" -status "$currentState"
  67. }
  68. }
  69.  
  70. # Evaluate Last Changed Device Event
  71. ::DeviceEvents::Evaluate 3 -name "$name" -status "$currentState" -lastChanged "[dict get $tempDict lastChanged]"
  72.  
  73. # Shouldn't Need This:
  74. #if {$currentState != $oldState} {
  75. # LOG "miDoor State Has Changed!"
  76. # LOG "Sending Received miDoor State Response to URC Interfaces"
  77. # LOG "$tempDict"
  78. # ::Access::Devices::Update -name "$name" -status [dict get $tempDict state]
  79. #}
  80. }
  81. }
  82. }
  83. if {[eof $sockID]} {
  84. LOG "WARNING: miDoor Closed the Socket Connection"
  85. catch {chan close $sockID}
  86. }
  87. }