fix: rebuild MES as compilable go-zero+ent backend (renamed bj_power_mes), restore 3 workstation projects from pristine original, align naming; all Go projects go build clean
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Beijing Hardman Automation Equipment Co., LTD. All rights reserved.
|
||||
*
|
||||
* Project: adapter
|
||||
* File: address.go
|
||||
* Last: 2025-05-09 16:46:26
|
||||
* Author: wangcheng@bj-hardman.com
|
||||
*/
|
||||
|
||||
package preload
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"bjhardman.cn/bjhardman/goplc/s7"
|
||||
)
|
||||
|
||||
type MAddress struct {
|
||||
LineId int
|
||||
Name string
|
||||
Desc string
|
||||
ReplyOffset int
|
||||
Addr s7.Address
|
||||
}
|
||||
|
||||
func (m MAddress) Copy() MAddress {
|
||||
return MAddress{
|
||||
LineId: m.LineId,
|
||||
Name: m.Name,
|
||||
Desc: m.Desc,
|
||||
ReplyOffset: m.ReplyOffset,
|
||||
Addr: m.Addr,
|
||||
}
|
||||
}
|
||||
|
||||
func (m MAddress) String() string {
|
||||
return fmt.Sprintf("%s(%s)", m.Addr, m.Name)
|
||||
}
|
||||
|
||||
func (m MAddress) Address() string {
|
||||
return m.Addr.String()
|
||||
}
|
||||
func (m MAddress) GetReplyAddress() string {
|
||||
return m.Addr.Add(0, m.ReplyOffset).String()
|
||||
}
|
||||
func (m MAddress) GetReplyMAddress() MAddress {
|
||||
return MAddress{
|
||||
LineId: m.LineId,
|
||||
Name: m.Name,
|
||||
Desc: m.Desc,
|
||||
ReplyOffset: 0,
|
||||
Addr: m.Addr.Add(0, m.ReplyOffset),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Beijing Hardman Automation Equipment Co., LTD. All rights reserved.
|
||||
*
|
||||
* Project: adapter
|
||||
* File: address_test.go
|
||||
* Last: 2025-05-17 15:27:01
|
||||
* Author: wangcheng@bj-hardman.com
|
||||
*/
|
||||
|
||||
package preload
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"bjhardman.cn/bjhardman/goplc/s7"
|
||||
)
|
||||
|
||||
func TestAddress(t *testing.T) {
|
||||
addr := MAddress{
|
||||
LineId: 1,
|
||||
Name: "ttt",
|
||||
Desc: "xxx",
|
||||
Addr: s7.Address{
|
||||
Area: "M",
|
||||
DBNumber: 0,
|
||||
DataType: "",
|
||||
Offset: 100,
|
||||
BitOffset: 0,
|
||||
},
|
||||
}
|
||||
|
||||
t.Log(addr.String())
|
||||
t.Log(addr.Address())
|
||||
addr.Addr.Add(0, 1)
|
||||
t.Log(addr.String())
|
||||
t.Log(addr.Address())
|
||||
t.Log(addr.Addr.Add(0, 1))
|
||||
t.Log(addr.String())
|
||||
t.Log(addr.Address())
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package preload
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
)
|
||||
|
||||
var (
|
||||
productTypeCategories = make(map[int]string) // ProductTypeID → Category
|
||||
productTypeCategoriesMu sync.RWMutex
|
||||
)
|
||||
|
||||
// InitProductTypeCategories 启动时加载所有 ProductType ID → Category 映射到内存
|
||||
func InitProductTypeCategories(entClient *ent.Client) {
|
||||
pts, err := entClient.ProductType.Query().All(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
m := make(map[int]string, len(pts))
|
||||
for _, pt := range pts {
|
||||
m[pt.ID] = string(pt.Category)
|
||||
}
|
||||
productTypeCategoriesMu.Lock()
|
||||
productTypeCategories = m
|
||||
productTypeCategoriesMu.Unlock()
|
||||
}
|
||||
|
||||
// GetCategory 返回 ProductTypeID 对应的 Category,未找到返回空字符串
|
||||
func GetCategory(productTypeID int) string {
|
||||
productTypeCategoriesMu.RLock()
|
||||
defer productTypeCategoriesMu.RUnlock()
|
||||
return productTypeCategories[productTypeID]
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Beijing Hardman Automation Equipment Co., LTD. All rights reserved.
|
||||
*
|
||||
* Project: adapter
|
||||
* File: signal.go
|
||||
* Last: 2025-01-11 11:22:28
|
||||
* Author: wangcheng@bj-hardman.com
|
||||
*/
|
||||
|
||||
package preload
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"sync"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
|
||||
"bjhardman.cn/bjhardman/goplc/s7"
|
||||
)
|
||||
|
||||
var (
|
||||
signals = make(map[string]MAddress)
|
||||
signalsMu sync.RWMutex
|
||||
)
|
||||
|
||||
func Init(entClient *ent.Client) {
|
||||
slog.Info("start to preload signals")
|
||||
ss, err := entClient.Signal.Query().All(context.Background())
|
||||
if err != nil {
|
||||
slog.Error("查询信号数据失败", "error", err.Error())
|
||||
panic(err)
|
||||
}
|
||||
|
||||
loaded := make(map[string]MAddress, len(ss))
|
||||
validSignals := 0
|
||||
for _, s := range ss {
|
||||
addr, err := s7.ParseAddress(s.Address)
|
||||
if err != nil {
|
||||
slog.Error("解析PLC地址失败,跳过该信号", "signal", s.Name, "address", s.Address, "error", err.Error())
|
||||
panic(err)
|
||||
}
|
||||
loaded[s.Name] = MAddress{
|
||||
Name: s.Name,
|
||||
Desc: s.Description,
|
||||
Addr: *addr,
|
||||
ReplyOffset: s.ReplyOffset,
|
||||
}
|
||||
validSignals++
|
||||
}
|
||||
|
||||
signalsMu.Lock()
|
||||
signals = loaded
|
||||
signalsMu.Unlock()
|
||||
|
||||
slog.Info("preload signals successfully", "total", len(ss), "valid", validSignals)
|
||||
}
|
||||
|
||||
func GetMAddress(name string) MAddress {
|
||||
signalsMu.RLock()
|
||||
defer signalsMu.RUnlock()
|
||||
return signals[name]
|
||||
}
|
||||
Reference in New Issue
Block a user