status.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. type PADQueue struct {
  12. Exten string `json:"exten"`
  13. ActiveTime time.Time `json:"activeTime"`
  14. }
  15. var PadQueues []PADQueue
  16. // 删除某个报警器
  17. func removeAllByExten(queues []PADQueue, target string) []PADQueue {
  18. result := make([]PADQueue, 0, len(queues))
  19. for _, q := range queues {
  20. if q.Exten != target {
  21. result = append(result, q)
  22. }
  23. }
  24. return result
  25. }
  26. // 添加报警器
  27. func addExtenToQueue(queues []PADQueue, target string) []PADQueue {
  28. var padInfo = PADQueue{Exten: target, ActiveTime: time.Now()}
  29. for _, q := range queues {
  30. if q.Exten == target {
  31. return queues
  32. }
  33. }
  34. queues = append(queues, padInfo)
  35. return queues
  36. }
  37. // 初始化pad队列
  38. func init() {
  39. PadQueues = make([]PADQueue, 0)
  40. }
  41. func SendToStc(conn net.Conn, data []byte) {
  42. _, err := conn.Write(data)
  43. if err != nil {
  44. utils.LoggerDebug.Printf("Send To STC msg err:%+v", err)
  45. conn.Close()
  46. }
  47. //lfshook.NewLogger().Logger.Infof("SendToStc data:%x", data)
  48. }
  49. // report alarm status to STC
  50. func AlarmStatus(exten string, status string) {
  51. //Not Master role , ignore
  52. if !active.Master {
  53. return
  54. }
  55. //check exten if it is a alarm exten
  56. if !utils.IsPAIU(exten) { // if not alarm device , return
  57. return
  58. }
  59. protocol := msgdata.NewProtocol()
  60. protocol.MessageID = 0x26
  61. protocol.DataLength = 0x04
  62. protocol.Data = make([]byte, 4)
  63. protocol.Data[0] = exten[2] - '0' //车厢号
  64. protocol.Data[1] = exten[3] - '0' //位置号
  65. //报警器工作状态
  66. switch status {
  67. case "unavailable", "Unavailable": //offline
  68. protocol.Data[2] = 0x00
  69. case "idle", "Idle": //idle
  70. protocol.Data[2] = 0x01
  71. case "dial": //dial
  72. protocol.Data[2] = 0x02
  73. return
  74. case "queue": //PAD alarm
  75. protocol.Data[2] = 0x03
  76. case "connect": //connect
  77. protocol.Data[2] = 0x04
  78. case "allreset": //allreset
  79. protocol.MessageID = 0x29
  80. protocol.Data[0] = 0x02 //all reset
  81. protocol.Data[1] = 0
  82. protocol.Data[2] = 0
  83. case "allhold": //allhold
  84. protocol.MessageID = 0x29
  85. protocol.Data[0] = 0x01 //all hold
  86. protocol.Data[1] = 0
  87. protocol.Data[2] = 0
  88. }
  89. //将pad存储到队列中
  90. if protocol.Data[2] == 0x03 { //排队状态
  91. PadQueues = addExtenToQueue(PadQueues, exten)
  92. utils.LoggerDebug.Printf("PAD:%s add to queue", exten)
  93. } else if protocol.Data[2] == 0x01 { //空闲状态
  94. utils.LoggerDebug.Printf("PAD:%s del from queue", exten)
  95. PadQueues = removeAllByExten(PadQueues, exten)
  96. }
  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", exten, protocol.Data[0], protocol.Data[1], protocol.Data[2])
  104. if socket.Conn != nil {
  105. SendToStc(socket.Conn, encoded)
  106. }
  107. if socket.Conn8 != nil {
  108. SendToStc(socket.Conn8, encoded)
  109. }
  110. }
  111. // report broadcast status to STC
  112. func PaStatus(src string, patype string, operation string) {
  113. //Not Master role , ignore
  114. if !active.Master {
  115. return
  116. }
  117. utils.LoggerDebug.Printf("PA Status Src:%s Type:%s Status:%s", src, patype, operation)
  118. protocol := msgdata.NewProtocol()
  119. protocol.MessageID = 0x22
  120. protocol.DataLength = 0x04
  121. protocol.Data = make([]byte, 4)
  122. //广播发起方
  123. switch src {
  124. case "2311": //mc1
  125. protocol.Data[0] = 0x01
  126. case "2381": //mc8
  127. protocol.Data[0] = 0x08
  128. /*case "1411": //mc8
  129. protocol.Data[0] = 0x08
  130. case "1481": //mc8
  131. protocol.Data[0] = 0x08*/
  132. default: //
  133. protocol.Data[0] = 0x00
  134. }
  135. //广播类型
  136. switch patype {
  137. case "C2C": //司机对讲---ami
  138. protocol.Data[1] = 0x01
  139. case "PA": //人工广播---ami
  140. protocol.Data[1] = 0x02
  141. case "DCS": //开关门提示音
  142. protocol.Data[1] = 0x03
  143. case "EMG": //紧急广播
  144. protocol.Data[1] = 0x04
  145. case "PAD": //报警
  146. protocol.Data[1] = 0x05
  147. case "CPA": //地面广播---ami
  148. protocol.Data[1] = 0x06
  149. case "SPC": //特殊
  150. protocol.Data[1] = 0x07
  151. case "STN": //报站
  152. protocol.Data[1] = 0x08
  153. case "CHK": //自检
  154. protocol.Data[1] = 0x09
  155. case "VOL": //音调条件
  156. protocol.Data[1] = 0x0a
  157. }
  158. //操作类型
  159. switch operation {
  160. case "start": //
  161. protocol.Data[2] = 0x01
  162. case "end": //
  163. protocol.Data[2] = 0x02
  164. case "refuse": //
  165. protocol.Data[2] = 0x03
  166. case "fail": //
  167. protocol.Data[2] = 0x04
  168. case "continue": //
  169. protocol.Data[2] = 0x05
  170. }
  171. encoded, errEn := protocol.Encode()
  172. if errEn != nil {
  173. utils.LoggerDebug.Printf("Encode error:%+v", errEn)
  174. return
  175. }
  176. if socket.Conn != nil {
  177. SendToStc(socket.Conn, encoded)
  178. }
  179. if socket.Conn8 != nil {
  180. SendToStc(socket.Conn8, encoded)
  181. }
  182. }
  183. // report broadcast status to STC
  184. func OccPad(operation string) {
  185. //Not Master role , ignore
  186. if !active.Master {
  187. return
  188. }
  189. utils.LoggerDebug.Printf("OCC-PAD status:%s", operation)
  190. protocol := msgdata.NewProtocol()
  191. protocol.MessageID = 0x2A
  192. protocol.DataLength = 0x04
  193. protocol.Data = make([]byte, 4)
  194. //广播发起方
  195. switch operation {
  196. case "start": //mc1
  197. protocol.Data[0] = 0x01
  198. case "end": //mc8
  199. protocol.Data[0] = 0x02
  200. case "": //
  201. protocol.Data[0] = 0x00
  202. }
  203. encoded, errEn := protocol.Encode()
  204. if errEn != nil {
  205. utils.LoggerDebug.Printf("Encode error:%+v", errEn)
  206. return
  207. }
  208. if socket.Conn != nil {
  209. SendToStc(socket.Conn, encoded)
  210. }
  211. if socket.Conn8 != nil {
  212. SendToStc(socket.Conn8, encoded)
  213. }
  214. }
  215. // report broadcast status to STC
  216. func SendRecordFile(filename, rcdtype string) {
  217. //time.Sleep(5 * time.Second)
  218. /*
  219. if !utils.FileExists(filename) {
  220. lfshook.NewLogger().Logger.Infof("===Recording filename not exist:%+v=", filename)
  221. return
  222. }
  223. */
  224. //Not Master role , ignore
  225. if !active.Master {
  226. return
  227. }
  228. protocol := msgdata.NewProtocol()
  229. protocol.MessageID = 0x31
  230. filenameHex := []byte(filename)
  231. dataLen := len(filenameHex) + 1
  232. protocol.DataLength = uint16(dataLen)
  233. protocol.Data = make([]byte, dataLen)
  234. copy(protocol.Data[1:], filenameHex)
  235. switch rcdtype {
  236. case "C2C": //
  237. protocol.Data[0] = 0x01
  238. case "PA": //
  239. protocol.Data[0] = 0x02
  240. case "PAD": //
  241. protocol.Data[0] = 0x05
  242. case "CPA": //
  243. protocol.Data[0] = 0x06
  244. case "OTR": //
  245. protocol.Data[0] = 0x03
  246. //lfshook.NewLogger().Logger.Infof("===Recording filename:%+v=", protocol.Data)
  247. }
  248. encoded, errEn := protocol.Encode()
  249. if errEn != nil {
  250. utils.LoggerDebug.Printf("Encode error:%+v", errEn)
  251. return
  252. }
  253. if socket.Conn != nil {
  254. SendToStc(socket.Conn, encoded)
  255. }
  256. if socket.Conn8 != nil {
  257. SendToStc(socket.Conn8, encoded)
  258. }
  259. }