@@ -37,9 +37,18 @@ const (
3737 BufLen = 4096
3838 SerializePlain = 0
3939 SerializeJson = 1
40+ FSStatusReply = `UP %d years, %d days, %d hours, %d minutes, %d seconds, %d milliseconds, %d microseconds
41+ FreeSWITCH (Version 1.8.7 git 6047ebd 2019-07-02 20:06:09Z 64bit) is ready
42+ 0 session(s) since startup
43+ 0 session(s) - peak 0, last 5min 0
44+ 0 session(s) per Sec out of max 30, peak 0, last 5min 0
45+ 1000 session(s) max
46+ min idle cpu 0.00/99.73
47+ Current Stack Size/Max 240K/8192K`
4048)
4149
4250type Worker struct {
51+ startTime time.Time
4352 stop bool
4453 conn net.Conn
4554 pass string
@@ -60,6 +69,7 @@ func NewWorker(conn net.Conn, pass, uuid string, events chan *Event) *Worker {
6069 resUuid = tryUuid
6170 }
6271 return & Worker {
72+ startTime : time .Now (),
6373 conn : conn ,
6474 pass : pass ,
6575 events : make ([]string , 0 ),
@@ -160,6 +170,17 @@ func (fs *Worker) processCommand(s string) {
160170 fs .customEvents = append (fs .customEvents , events [i ])
161171 }
162172 }
173+ case "api" , "bgapi" :
174+ var reply string
175+ if args [0 ] == "status" {
176+ Y , M , D , H , m , sec := diff (time .Now (), fs .startTime )
177+ reply = fmt .Sprintf (FSStatusReply , Y , M , D , H , m , sec , 0 )
178+ } else {
179+ reply = FsErrCommandNotFound
180+ }
181+ if _ , err := fs .conn .Write ([]byte (reply )); err != nil {
182+ fs .stop = true
183+ }
163184 default :
164185 if _ , err := fs .conn .Write ([]byte (FsErrCommandNotFound )); err != nil {
165186 fs .stop = true
@@ -220,3 +241,50 @@ func (fs *Worker) sendMessage(buf string) error {
220241 }
221242 return nil
222243}
244+
245+ func diff (a , b time.Time ) (year , month , day , hour , min , sec int ) {
246+ if a .Location () != b .Location () {
247+ b = b .In (a .Location ())
248+ }
249+ if a .After (b ) {
250+ a , b = b , a
251+ }
252+ y1 , M1 , d1 := a .Date ()
253+ y2 , M2 , d2 := b .Date ()
254+
255+ h1 , m1 , s1 := a .Clock ()
256+ h2 , m2 , s2 := b .Clock ()
257+
258+ year = int (y2 - y1 )
259+ month = int (M2 - M1 )
260+ day = int (d2 - d1 )
261+ hour = int (h2 - h1 )
262+ min = int (m2 - m1 )
263+ sec = int (s2 - s1 )
264+
265+ // Normalize negative values
266+ if sec < 0 {
267+ sec += 60
268+ min --
269+ }
270+ if min < 0 {
271+ min += 60
272+ hour --
273+ }
274+ if hour < 0 {
275+ hour += 24
276+ day --
277+ }
278+ if day < 0 {
279+ // days in month:
280+ t := time .Date (y1 , M1 , 32 , 0 , 0 , 0 , 0 , time .UTC )
281+ day += 32 - t .Day ()
282+ month --
283+ }
284+ if month < 0 {
285+ month += 12
286+ year --
287+ }
288+
289+ return
290+ }
0 commit comments