123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- package api
- import (
- "fmt"
- "launcher/cmd"
- "launcher/conf"
- "time"
- "github.com/energye/energy/v2/cef"
- "github.com/energye/energy/v2/cef/ipc"
- "github.com/energye/golcl/lcl"
- "github.com/energye/golcl/lcl/types"
- )
- type RunningApp struct {
- Guid string
- Title string
- Key string
- }
- type RunningNativeApp struct {
- Key string
- Entry string
- Params []string
- StartTime time.Time
- Guid string
- Title string
- Cmd *cmd.NativeExe
- }
- type RunningCmdSvcApp struct {
- Entry string
- Params []string
- StartTime time.Time
- Guid string
- Title string
- Key string
- Web *cef.LCLBrowserWindow
- Cmd *cmd.CmdSvcExe
- }
- var running_natives = make(map[string]*RunningNativeApp)
- var running_svcs = make(map[string]*RunningCmdSvcApp)
- func StopRunningApp() {
- for k, app := range running_natives {
- app.Cmd.Stop()
- delete(running_natives, k)
- }
- }
- func GetAppName(guid string) string {
- for _, app := range running_natives {
- if app.Guid == guid {
- return app.Title
- }
- }
- return ""
- }
- func _runNativeApp(guid, entryFile string, params []string, title string, key string) string {
- params = append(params,
- fmt.Sprintf("--port=%d", conf.AppConfig.NatsWsPort),
- fmt.Sprintf("--data=%s", conf.AppConfig.DataDir),
- fmt.Sprintf("--guid=%s", guid))
- cmd := &cmd.NativeExe{
- ExeFile: entryFile,
- Params: params,
- MonitorQuit: true,
- Quit: make(chan int),
- }
- err := cmd.Start()
- if err != nil {
- fmt.Println("start ", entryFile, "error", err.Error())
- return err.Error()
- }
- go func() {
- qflag := <-cmd.Quit
- fmt.Println("native app quite ", qflag)
- if running_natives[guid] != nil {
- delete(running_natives, guid)
- }
- StopApp(guid)
- }()
- running_natives[guid] = &RunningNativeApp{
- Guid: guid,
- Key: key,
- Entry: entryFile,
- Params: params,
- StartTime: time.Now(),
- Cmd: cmd,
- Title: title,
- }
- StartApp(guid)
- return ""
- }
- func RegRunApp(event *cef.BrowserEvent, window cef.IBrowserWindow, bw *cef.LCLBrowserWindow) {
- ipc.On("GetRunningApp", func() []RunningApp {
- out := []RunningApp{}
- for _, app := range running_natives {
- out = append(out, RunningApp{Guid: app.Guid, Title: app.Title, Key: app.Key})
- }
- for _, app := range running_svcs {
- out = append(out, RunningApp{Guid: app.Guid, Title: app.Title, Key: app.Key})
- }
- fmt.Println(running_svcs)
- return out
- })
- ipc.On("RunCmdSvcApp", func(guid, webUrl, entryFile string, params []string, title string, key string) string {
- window.RunOnMainThread(func() {
- params = append(params,
- fmt.Sprintf("--port=%d", conf.AppConfig.NatsWsPort),
- fmt.Sprintf("--data=%s", conf.AppConfig.DataDir),
- fmt.Sprintf("--guid=%s", guid))
- b := NewWeb(webUrl, entryFile, 1280, 720)
- app := &RunningCmdSvcApp{
- Guid: guid,
- Entry: entryFile,
- Params: params,
- Key: key,
- Title: title,
- StartTime: time.Now(),
- Web: b,
- }
- cmdsvc := &cmd.CmdSvcExe{
- NatsMsgName: fmt.Sprintf("%s.cmdmsg", guid),
- ExeFile: entryFile,
- Params: params,
- MonitorQuit: true,
- Quit: make(chan int),
- }
- err := cmdsvc.Start()
- if err != nil {
- fmt.Println("start ", entryFile, "error", err.Error())
- return
- }
- quiting := 0
- cmdsvcQuited := 0
- go func() {
- qflag := <-cmdsvc.Quit
- fmt.Println("cmd svc app quite ", qflag)
- if running_svcs[guid] != nil {
- delete(running_svcs, guid)
- }
- StopApp(guid)
- if quiting == 0 {
- cmdsvcQuited = 1
- b.Close()
- }
- }()
- if b != nil {
- b.SetOnClose(func(sender lcl.IObject, action *types.TCloseAction) bool {
- if cmdsvcQuited == 0 {
- quiting = 1
- cmdsvc.Stop()
- fmt.Println("web closed")
- }
- *action = types.CaFree
- return false
- })
- }
- running_svcs[guid] = app
- StartApp(guid)
- })
- return ""
- })
- ipc.On("RunNativeApp", _runNativeApp)
- ipc.On("StopNativeApp", func(guid string) bool {
- running := running_natives[guid]
- if running == nil {
- return true
- }
- running.Cmd.Stop()
- return true
- })
- }
|