80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package main
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"embed"
|
||
|
|
"flag"
|
||
|
|
"fmt"
|
||
|
|
"io/fs"
|
||
|
|
|
||
|
|
"bj_power_workstation/internal/config"
|
||
|
|
"bj_power_workstation/internal/handler"
|
||
|
|
"bj_power_workstation/internal/mes"
|
||
|
|
"bj_power_workstation/internal/store"
|
||
|
|
"bj_power_workstation/internal/svc"
|
||
|
|
"bj_power_workstation/internal/syncer"
|
||
|
|
"bj_power_workstation/internal/tightening"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/conf"
|
||
|
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
|
"github.com/zeromicro/go-zero/rest"
|
||
|
|
)
|
||
|
|
|
||
|
|
//go:embed all:web/static
|
||
|
|
var webFS embed.FS
|
||
|
|
|
||
|
|
var configFile = flag.String("f", "etc/bj_power_workstation.yaml", "the config file")
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
flag.Parse()
|
||
|
|
|
||
|
|
var c config.Config
|
||
|
|
conf.MustLoad(*configFile, &c)
|
||
|
|
|
||
|
|
staticFS, err := fs.Sub(webFS, "web/static")
|
||
|
|
if err != nil {
|
||
|
|
panic(fmt.Sprintf("加载内嵌前端资源失败: %v", err))
|
||
|
|
}
|
||
|
|
|
||
|
|
// 本地 SQLite:拧紧数据缓存 + 离线上报队列 + 内置账号
|
||
|
|
st, err := store.Open(c.Sqlite.Path)
|
||
|
|
if err != nil {
|
||
|
|
panic(fmt.Sprintf("打开本地 SQLite(%s) 失败: %v", c.Sqlite.Path, err))
|
||
|
|
}
|
||
|
|
defer st.Close()
|
||
|
|
|
||
|
|
if err := store.SeedIfEmpty(st); err != nil {
|
||
|
|
panic(fmt.Sprintf("初始化内置用户失败: %v", err))
|
||
|
|
}
|
||
|
|
|
||
|
|
rootCtx, cancel := context.WithCancel(context.Background())
|
||
|
|
defer cancel()
|
||
|
|
|
||
|
|
mc := mes.New(c.Mes.BaseURL, c.Internal.Token)
|
||
|
|
|
||
|
|
// 后台同步协程:联网时向 MES 补报未同步数据
|
||
|
|
sy := syncer.New(st, mc)
|
||
|
|
go sy.Run(rootCtx)
|
||
|
|
|
||
|
|
// 内置模拟拧紧枪数据源(Sim.Enable=true 时启动;正式丹尼科尔协议接入后由 NetSource 替代)
|
||
|
|
if c.Sim.Enable {
|
||
|
|
go tightening.NewMockSource(st, mc).Start(rootCtx)
|
||
|
|
}
|
||
|
|
|
||
|
|
ctx := &svc.ServiceContext{
|
||
|
|
Config: c,
|
||
|
|
Store: st,
|
||
|
|
Mes: mc,
|
||
|
|
Syncer: sy,
|
||
|
|
WebFS: staticFS,
|
||
|
|
}
|
||
|
|
|
||
|
|
server := rest.MustNewServer(c.RestConf)
|
||
|
|
defer server.Stop()
|
||
|
|
handler.RegisterHandlers(server, ctx)
|
||
|
|
|
||
|
|
fmt.Printf("Starting bj_power_workstation server at %s:%d...\n", c.Host, c.Port)
|
||
|
|
logx.Infof("Starting bj_power_workstation server at %s:%d...", c.Host, c.Port)
|
||
|
|
server.Start()
|
||
|
|
}
|