优化系统
This commit is contained in:
+95
-1
@@ -11,17 +11,18 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00")
|
||||
fmt.Println(date, date.Unix())
|
||||
//fmt.Println("0123456"[1:7])
|
||||
appIns := hotime.Init("config/config.json")
|
||||
go runTcpServer(&appIns) //运行tcp监测,产线监测使用
|
||||
//RESTfull接口适配
|
||||
appIns.SetConnectListener(func(context *hotime.Context) bool {
|
||||
|
||||
@@ -313,3 +314,96 @@ func main() {
|
||||
//},
|
||||
})
|
||||
}
|
||||
func Process(conn net.Conn, appIns *hotime.Application) {
|
||||
// 循环接收客户端发送的数据
|
||||
client := conn.RemoteAddr().String() // 客户端IP:port
|
||||
client = client[:strings.Index(client, ":")]
|
||||
defer conn.Close() // 关闭conn
|
||||
for {
|
||||
// 创建一个新的切片
|
||||
buf := make([]byte, 1024)
|
||||
// fmt.Printf("服务器在等待客户端%s发送信息\n", conn.RemoteAddr().String())
|
||||
n, err := conn.Read(buf) // 从conn中读取
|
||||
// 等待客户端通过conn发送信息,
|
||||
// 如果客户端没有发送(write),就会阻塞在这里
|
||||
if err != nil {
|
||||
// 一般为这个err
|
||||
fmt.Printf("客户端%s已退出..\n", client)
|
||||
return
|
||||
}
|
||||
// 显示客户端发送的内容到服务器的终端
|
||||
sn := string(buf[:n])
|
||||
sn = strings.Replace(sn, "\r", "", -1)
|
||||
fmt.Println(client, sn) // 读到了n个数据
|
||||
|
||||
if len(sn) < 3 {
|
||||
//that.Display(3, "参数不足,请补充参数")
|
||||
fmt.Println(client, sn, "参数过短") // 读到了n个数据
|
||||
continue
|
||||
}
|
||||
|
||||
vs := appIns.Db.Select("produce", "sn,id,product_id",
|
||||
common.Map{"AND": common.Map{"produce.sn[~]": sn[:len(sn)/2+1], "state[!]": 0}})
|
||||
produce := common.Map{"ld": 10000}
|
||||
for _, v := range vs {
|
||||
ld := common.StrLd(v.GetString("sn"), sn, true)
|
||||
if ld < produce.GetCeilInt("ld") {
|
||||
v["ld"] = ld
|
||||
produce = v
|
||||
}
|
||||
}
|
||||
|
||||
oldSn := appIns.Db.Get("produce_product", "id", common.Map{"sn": sn, "produce_id": produce.GetCeilInt("id")})
|
||||
if oldSn != nil {
|
||||
fmt.Println(client, sn, "已经添加请勿重复添加") // 读到了n个数据
|
||||
continue
|
||||
|
||||
}
|
||||
|
||||
data := common.Map{
|
||||
"sn": sn,
|
||||
"product_id": produce.GetCeilInt("product_id"),
|
||||
"produce_id": produce.GetCeilInt("id"),
|
||||
"create_time": time.Now().Unix(),
|
||||
"modify_time": time.Now().Unix(),
|
||||
}
|
||||
|
||||
productLine := appIns.Db.Get("product_line", "id", common.Map{"ipaddr": client})
|
||||
|
||||
if productLine != nil {
|
||||
data["product_line_id"] = productLine.GetCeilInt("id")
|
||||
}
|
||||
|
||||
id := appIns.Db.Insert("produce_product", data)
|
||||
if id == 0 {
|
||||
//that.Display(4, "添加新成品失败,请重新添加")
|
||||
fmt.Println(client, sn, "添加新成品失败,请重新添加") // 读到了n个数据
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runTcpServer(appIns *hotime.Application) {
|
||||
|
||||
listen, err := net.Listen("tcp", "0.0.0.0:10000")
|
||||
if err != nil {
|
||||
fmt.Println("listen err =", err)
|
||||
return
|
||||
}
|
||||
defer listen.Close() // 延时关闭listen
|
||||
fmt.Println("listening success:", listen.Addr())
|
||||
|
||||
// 循环等待客户端来连接
|
||||
fmt.Println("等待客户端来连接..")
|
||||
for {
|
||||
conn, err := listen.Accept()
|
||||
if err != nil {
|
||||
fmt.Println("Accept() err =", err)
|
||||
} else {
|
||||
fmt.Printf("客户端%s已连接..\n", conn.RemoteAddr().String())
|
||||
}
|
||||
// 准备一个协程,为客户端服务
|
||||
go Process(conn, appIns)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user