status.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package alstatus
  2. import (
  3. "fmt"
  4. "net"
  5. "pbx-api-gin/internal/app/stc/active"
  6. msgdata "pbx-api-gin/internal/app/stc/data"
  7. "pbx-api-gin/internal/app/stc/socket"
  8. "pbx-api-gin/pkg/utils"
  9. "time"
  10. )
  11. func SendToStc(conn net.Conn, data []byte) {
  12. _, err := conn.Write(data)
  13. if err != nil {
  14. utils.LoggerDebug.Printf("Send To STC msg err:%+v", err)
  15. conn.Close()
  16. }
  17. //lfshook.NewLogger().Logger.Infof("SendToStc data:%x", data)
  18. }
  19. // report alarm status to STC
  20. func SendQueueStatus(pads string, count int) {
  21. if !active.CheckReunoinMode() {
  22. return
  23. }
  24. //Not Master role , ignore
  25. if !active.Master {
  26. return
  27. }
  28. DataLen := len(pads) + 2
  29. protocol := msgdata.NewProtocol()
  30. protocol.MessageID = 0x29
  31. protocol.DataLength = uint16(DataLen)
  32. protocol.Data = make([]byte, DataLen)
  33. protocol.Data[0] = byte(count) //排队数量
  34. copy(protocol.Data[1:], []byte(pads))
  35. encoded, errEn := protocol.Encode()
  36. if errEn != nil {
  37. fmt.Println("Encode error:", errEn)
  38. return
  39. }
  40. //check if actived
  41. utils.LoggerDebug.Printf("PADs number:%s count:%d ", protocol.Data[1:], protocol.Data[0])
  42. if socket.Conn != nil {
  43. SendToStc(socket.Conn, encoded)
  44. }
  45. if socket.Conn6 != nil {
  46. SendToStc(socket.Conn6, encoded)
  47. }
  48. }
  49. // report alarm status to STC
  50. func AlarmStatus(exten string, status string) {
  51. if !active.CheckReunoinMode() {
  52. return
  53. }
  54. //Not Master role , ignore
  55. if !active.Master {
  56. return
  57. }
  58. //check exten if it is a alarm exten
  59. if !utils.IsPAIU(exten) { // if not alarm device , return
  60. return
  61. }
  62. protocol := msgdata.NewProtocol()
  63. protocol.MessageID = 0x26
  64. protocol.DataLength = 0x1E
  65. protocol.Data = make([]byte, 30)
  66. protocol.Data[0] = exten[2] - '0' //车厢号
  67. protocol.Data[1] = exten[3] - '0' //位置号
  68. //报警器工作状态
  69. switch status {
  70. case "unavailable", "Unavailable": //offline
  71. protocol.Data[2] = 0x00
  72. case "idle", "Idle": //idle
  73. protocol.Data[2] = 0x01
  74. case "dial": //dial
  75. protocol.Data[2] = 0x02
  76. return
  77. case "queue": //PAD alarm
  78. protocol.Data[2] = 0x03
  79. case "connect": //connect
  80. protocol.Data[2] = 0x04
  81. case "allreset": //allreset
  82. protocol.MessageID = 0x29
  83. protocol.Data[0] = 0x02 //all reset
  84. protocol.Data[1] = 0
  85. protocol.Data[2] = 0
  86. case "allhold": //allhold
  87. protocol.MessageID = 0x29
  88. protocol.Data[0] = 0x01 //all hold
  89. protocol.Data[1] = 0
  90. protocol.Data[2] = 0
  91. }
  92. //填充时间
  93. now := time.Now()
  94. ntm := now.Format("2006-01-02 15:04:05")
  95. //fmt.Println(s)
  96. copy(protocol.Data[3:], []byte(ntm))
  97. encoded, errEn := protocol.Encode()
  98. if errEn != nil {
  99. fmt.Println("Encode error:", errEn)
  100. return
  101. }
  102. //check if actived
  103. utils.LoggerDebug.Printf("PAD number:%s CarNum:%x Pos:%x Status:%x(0=Offline,1=Idle,2=calling,3=Hold,4=Connected)", exten, protocol.Data[0], protocol.Data[1], protocol.Data[2])
  104. if socket.Conn != nil {
  105. SendToStc(socket.Conn, encoded)
  106. }
  107. if socket.Conn6 != nil {
  108. SendToStc(socket.Conn6, encoded)
  109. }
  110. }
  111. // report alarm status to STC
  112. func SendICPStatus(exten string, status string) {
  113. if !active.CheckReunoinMode() {
  114. return
  115. }
  116. //Not Master role , ignore
  117. if !active.Master {
  118. return
  119. }
  120. //check exten if it is a alarm exten
  121. if !utils.IsPAIU(exten) { // if not alarm device , return
  122. return
  123. }
  124. protocol := msgdata.NewProtocol()
  125. protocol.MessageID = 0x25
  126. protocol.DataLength = 0x04
  127. protocol.Data = make([]byte, 4)
  128. protocol.Data[0] = exten[2] - '0' //车厢号
  129. protocol.Data[1] = exten[3] - '0' //位置号
  130. //报警器工作状态
  131. switch status {
  132. case "unavailable", "Unavailable": //offline
  133. protocol.Data[2] = 0x00
  134. case "idle", "Idle": //idle
  135. protocol.Data[2] = 0x01
  136. case "dial": //dial
  137. protocol.Data[2] = 0x02
  138. return
  139. case "connect": //PAD alarm
  140. protocol.Data[2] = 0x03
  141. }
  142. encoded, errEn := protocol.Encode()
  143. if errEn != nil {
  144. fmt.Println("Encode error:", errEn)
  145. return
  146. }
  147. //check if actived
  148. utils.LoggerDebug.Printf("PAD number:%s CarNum:%x Pos:%x Status:%x(0=Offline,1=Idle,2=calling,3=Hold,4=Connected)", exten, protocol.Data[0], protocol.Data[1], protocol.Data[2])
  149. if socket.Conn != nil {
  150. SendToStc(socket.Conn, encoded)
  151. }
  152. if socket.Conn6 != nil {
  153. SendToStc(socket.Conn6, encoded)
  154. }
  155. }
  156. // report broadcast status to STC
  157. func PaStatus(src string, patype string, operation string) {
  158. if !active.CheckReunoinMode() {
  159. return
  160. }
  161. //Not Master role , ignore
  162. if !active.Master {
  163. return
  164. }
  165. //过滤掉非EMG运行模式下的continue状态发送
  166. //if operation == "continue" {
  167. // taskName, _, _ := priority.RegistryTask.HighestPriorityRunningTask()
  168. // if taskName != "EMG" {
  169. // return
  170. // }
  171. //}
  172. utils.LoggerDebug.Printf("PA Status Src:%s Type:%s Status:%s", src, patype, operation)
  173. protocol := msgdata.NewProtocol()
  174. protocol.MessageID = 0x22
  175. protocol.DataLength = 0x04
  176. protocol.Data = make([]byte, 4)
  177. //广播发起方
  178. switch src {
  179. case "1211", "2111": //mc1
  180. protocol.Data[0] = 0x01
  181. case "1261": //mc8
  182. protocol.Data[0] = 0x08
  183. default: //
  184. protocol.Data[0] = 0x00
  185. }
  186. //广播类型
  187. switch patype {
  188. case "PA": //人工广播---ami
  189. protocol.Data[1] = 0x01
  190. case "STNA": //自动报站
  191. protocol.Data[1] = 0x02
  192. case "STNS": //手动报站
  193. protocol.Data[1] = 0x03
  194. case "SVM": //服务消息
  195. protocol.Data[1] = 0x04
  196. case "PMC": //远程广播
  197. protocol.Data[1] = 0x05
  198. case "SVA": //服务音频
  199. protocol.Data[1] = 0x06
  200. case "DCS": //开关门提示
  201. protocol.Data[1] = 0x07
  202. }
  203. //操作类型
  204. switch operation {
  205. case "start": //
  206. protocol.Data[2] = 0x01
  207. case "end": //
  208. protocol.Data[2] = 0x02
  209. case "refuse": //
  210. protocol.Data[2] = 0x03
  211. case "fail": //
  212. protocol.Data[2] = 0x04
  213. case "continue": //
  214. protocol.Data[2] = 0x05
  215. }
  216. encoded, errEn := protocol.Encode()
  217. if errEn != nil {
  218. utils.LoggerDebug.Printf("Encode error:%+v", errEn)
  219. return
  220. }
  221. if socket.Conn != nil {
  222. SendToStc(socket.Conn, encoded)
  223. }
  224. if socket.Conn6 != nil {
  225. SendToStc(socket.Conn6, encoded)
  226. }
  227. }
  228. // report broadcast status to STC
  229. func SendRecordFile(filename, rcdtype string) {
  230. if !active.CheckReunoinMode() {
  231. return
  232. }
  233. //time.Sleep(5 * time.Second)
  234. /*
  235. if !utils.FileExists(filename) {
  236. lfshook.NewLogger().Logger.Infof("===Recording filename not exist:%+v=", filename)
  237. return
  238. }
  239. */
  240. //Not Master role , ignore
  241. if !active.Master {
  242. return
  243. }
  244. protocol := msgdata.NewProtocol()
  245. protocol.MessageID = 0x31
  246. filenameHex := []byte(filename)
  247. dataLen := len(filenameHex) + 1
  248. protocol.DataLength = uint16(dataLen)
  249. protocol.Data = make([]byte, dataLen)
  250. copy(protocol.Data[1:], filenameHex)
  251. switch rcdtype {
  252. case "C2C": //
  253. protocol.Data[0] = 0x01
  254. case "PA": //
  255. protocol.Data[0] = 0x02
  256. case "PAD": //
  257. protocol.Data[0] = 0x05
  258. case "CPA": //
  259. protocol.Data[0] = 0x06
  260. case "OTR": //
  261. protocol.Data[0] = 0x03
  262. //lfshook.NewLogger().Logger.Infof("===Recording filename:%+v=", protocol.Data)
  263. }
  264. encoded, errEn := protocol.Encode()
  265. if errEn != nil {
  266. utils.LoggerDebug.Printf("Encode error:%+v", errEn)
  267. return
  268. }
  269. if socket.Conn != nil {
  270. SendToStc(socket.Conn, encoded)
  271. }
  272. if socket.Conn6 != nil {
  273. SendToStc(socket.Conn6, encoded)
  274. }
  275. }