status.go 6.7 KB

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