go fmt
This commit is contained in:
parent
b10aace99b
commit
1bcc46f458
|
@ -4,10 +4,10 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
"strconv"
|
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Traffic struct {
|
type Traffic struct {
|
||||||
|
@ -64,7 +64,6 @@ var conf Conf
|
||||||
var DEVICEDATAS Devicedatas
|
var DEVICEDATAS Devicedatas
|
||||||
var demoDevice Device
|
var demoDevice Device
|
||||||
|
|
||||||
|
|
||||||
var db, _ = sql.Open("sqlite3", "./db/gateway.db")
|
var db, _ = sql.Open("sqlite3", "./db/gateway.db")
|
||||||
|
|
||||||
// 加载配置文件
|
// 加载配置文件
|
||||||
|
@ -87,16 +86,14 @@ func init() {
|
||||||
}
|
}
|
||||||
DEVICEDATAS.Unlock()
|
DEVICEDATAS.Unlock()
|
||||||
|
|
||||||
|
demoDevice.ID = "deb03e28-ddcb-4e5d-aa31-d870a1f0f09b"
|
||||||
demoDevice.ID="deb03e28-ddcb-4e5d-aa31-d870a1f0f09b"
|
demoDevice.VERSION = "yunhorn_kgl_c8_v1"
|
||||||
demoDevice.VERSION="yunhorn_kgl_c8_v1"
|
|
||||||
demoDevice.DATA = make(map[string]interface{})
|
demoDevice.DATA = make(map[string]interface{})
|
||||||
for a:=0;a<8;a++ {
|
for a := 0; a < 8; a++ {
|
||||||
demoDevice.DATA["data"+strconv.Itoa(a+1)]=0
|
demoDevice.DATA["data"+strconv.Itoa(a+1)] = 0
|
||||||
}
|
}
|
||||||
demoDevice.ONLINE=true
|
demoDevice.ONLINE = true
|
||||||
log.Println("init demoDevice success:",demoDevice)
|
log.Println("init demoDevice success:", demoDevice)
|
||||||
|
|
||||||
|
|
||||||
dbdata_init()
|
dbdata_init()
|
||||||
|
|
||||||
|
@ -119,7 +116,7 @@ func dbdata_init() {
|
||||||
rows.Close()
|
rows.Close()
|
||||||
}
|
}
|
||||||
//在原本没有db文件的情况下调用 会报错
|
//在原本没有db文件的情况下调用 会报错
|
||||||
// rows.Close()
|
// rows.Close()
|
||||||
rows, err = db.Query("SELECT traffic FROM traffic t where datetime(t.create_time) > datetime('now','start of day','-1 day') order by id DESC limit 1 ")
|
rows, err = db.Query("SELECT traffic FROM traffic t where datetime(t.create_time) > datetime('now','start of day','-1 day') order by id DESC limit 1 ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
traffic_data.TRAFFIC = 0
|
traffic_data.TRAFFIC = 0
|
||||||
|
|
6
main.go
6
main.go
|
@ -3,8 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"github.com/robfig/cron"
|
"github.com/robfig/cron"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -18,7 +18,7 @@ func main() {
|
||||||
send_data(1001)
|
send_data(1001)
|
||||||
})
|
})
|
||||||
// devices := []Device{}
|
// devices := []Device{}
|
||||||
c.AddFunc("*/10, *, *, *, *, *", func(){
|
c.AddFunc("*/10, *, *, *, *, *", func() {
|
||||||
// log.Println(string(build_data(1001,devices)))
|
// log.Println(string(build_data(1001,devices)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ func main() {
|
||||||
go handleConn(conn)
|
go handleConn(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// select {}
|
// select {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -205,7 +205,7 @@ func seat_api(c *gin.Context) {
|
||||||
for _, i := range conf.GATEWAY_INFO.CONFIG {
|
for _, i := range conf.GATEWAY_INFO.CONFIG {
|
||||||
arr := get_seat_device_data(i.ID)
|
arr := get_seat_device_data(i.ID)
|
||||||
version, num := arr[0], arr[1]
|
version, num := arr[0], arr[1]
|
||||||
log.Println("conf.data:",i.DATA)
|
log.Println("conf.data:", i.DATA)
|
||||||
for k, j := range i.DATA {
|
for k, j := range i.DATA {
|
||||||
if DEVICEDATAS.DATA[version+"_"+num].DATA != nil {
|
if DEVICEDATAS.DATA[version+"_"+num].DATA != nil {
|
||||||
if DEVICEDATAS.DATA[version+"_"+num].DATA["data"+strconv.Itoa(k+1)] != nil {
|
if DEVICEDATAS.DATA[version+"_"+num].DATA["data"+strconv.Itoa(k+1)] != nil {
|
||||||
|
|
|
@ -21,7 +21,7 @@ func serial_run() {
|
||||||
port_arr = append(port_arr, ports_arr[i])
|
port_arr = append(port_arr, ports_arr[i])
|
||||||
}
|
}
|
||||||
// 解析串口配置
|
// 解析串口配置
|
||||||
if conf.OPENSERIAL{
|
if conf.OPENSERIAL {
|
||||||
for _, i := range port_arr {
|
for _, i := range port_arr {
|
||||||
var PARITY serial.Parity
|
var PARITY serial.Parity
|
||||||
switch conf.SERIALPORTS_LIST[i].PARITY {
|
switch conf.SERIALPORTS_LIST[i].PARITY {
|
||||||
|
|
17
tcpserver.go
17
tcpserver.go
|
@ -11,7 +11,6 @@ import (
|
||||||
// "go-study/socket/config"
|
// "go-study/socket/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func handleConn(conn net.Conn) {
|
func handleConn(conn net.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
@ -37,12 +36,12 @@ func handleConn(conn net.Conn) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case <-time.After(5000 * time.Millisecond):
|
case <-time.After(5000 * time.Millisecond):
|
||||||
n,err := conn.Write(heartbeat)
|
n, err := conn.Write(heartbeat)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
log.Println("write msg error!",err)
|
log.Println("write msg error!", err)
|
||||||
stopChan <- true
|
stopChan <- true
|
||||||
}else{
|
} else {
|
||||||
log.Println("write msg success!",n)
|
log.Println("write msg success!", n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,9 +59,9 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
||||||
//log.Println("kkdata:",kkdata)
|
//log.Println("kkdata:",kkdata)
|
||||||
case <-time.After(50 * time.Millisecond): //上面的ch如果一直没数据会阻塞,那么select也会检测其他case条件,检测到后3秒超时
|
case <-time.After(50 * time.Millisecond): //上面的ch如果一直没数据会阻塞,那么select也会检测其他case条件,检测到后3秒超时
|
||||||
// log.Println("超时")
|
// log.Println("超时")
|
||||||
if len(Rdata) >3{
|
if len(Rdata) > 3 {
|
||||||
readChan <- Rdata
|
readChan <- Rdata
|
||||||
Rdata = make([]byte,0)
|
Rdata = make([]byte, 0)
|
||||||
}
|
}
|
||||||
//strData := string(Rdata)
|
//strData := string(Rdata)
|
||||||
//Rdata = make([]byte,0)
|
//Rdata = make([]byte,0)
|
||||||
|
@ -84,7 +83,7 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
|
||||||
}
|
}
|
||||||
log.Println(DEVICEDATAS)
|
log.Println(DEVICEDATAS)
|
||||||
|
|
||||||
log.Println("read.data:",Bytes2Bits(data[3:4]))
|
log.Println("read.data:", Bytes2Bits(data[3:4]))
|
||||||
|
|
||||||
data_buf <- data
|
data_buf <- data
|
||||||
}
|
}
|
||||||
|
|
31
worker.go
31
worker.go
|
@ -4,9 +4,9 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"github.com/tarm/serial"
|
"github.com/tarm/serial"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"math/rand"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Devicedatas struct {
|
type Devicedatas struct {
|
||||||
|
@ -14,7 +14,6 @@ type Devicedatas struct {
|
||||||
DATA map[string]Device
|
DATA map[string]Device
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
@ -23,7 +22,7 @@ type Device struct {
|
||||||
DATA map[string]interface{} `json:"data"`
|
DATA map[string]interface{} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func yunhorn_ys_l_v1(data_buf []byte){
|
func yunhorn_ys_l_v1(data_buf []byte) {
|
||||||
|
|
||||||
// for _, i := range conf.RS485DEVICES {
|
// for _, i := range conf.RS485DEVICES {
|
||||||
// log.Println("################################",i)
|
// log.Println("################################",i)
|
||||||
|
@ -34,22 +33,26 @@ func yunhorn_ys_l_v1(data_buf []byte){
|
||||||
// log.Println(binary.BigEndian.Uint16(append([]byte{0x00}, k))
|
// log.Println(binary.BigEndian.Uint16(append([]byte{0x00}, k))
|
||||||
// }
|
// }
|
||||||
|
|
||||||
log.Println("ys_l_v1_data:",data_buf)
|
// log.Println("ys_l_v1_data:",data_buf)
|
||||||
data_int := Bytes2Bits(data_buf[3:4])
|
data_int := Bytes2Bits(data_buf[3:4])
|
||||||
|
data_int0 := Bytes2Bits(data_buf[0:1])
|
||||||
|
data_int1 := Bytes2Bits(data_buf[1:2])
|
||||||
|
data_int2 := Bytes2Bits(data_buf[2:3])
|
||||||
|
|
||||||
log.Println("data_int:",data_int[0])
|
log.Println("data_int:", data_int[0])
|
||||||
|
log.Printf("ADRH:%d,ADRL:%d,dataType:%d,", data_int0[0], data_int1[0], data_int2[0])
|
||||||
var device Device
|
var device Device
|
||||||
device.ID=demoDevice.ID
|
device.ID = demoDevice.ID
|
||||||
device.VERSION=demoDevice.VERSION
|
device.VERSION = demoDevice.VERSION
|
||||||
device.ONLINE=demoDevice.ONLINE
|
device.ONLINE = demoDevice.ONLINE
|
||||||
|
|
||||||
demoDevice.DATA["data"+strconv.Itoa(1)]=rand.Intn(2)
|
demoDevice.DATA["data"+strconv.Itoa(1)] = rand.Intn(2)
|
||||||
demoDevice.DATA["data"+strconv.Itoa(2)]=rand.Intn(2)
|
demoDevice.DATA["data"+strconv.Itoa(2)] = rand.Intn(2)
|
||||||
demoDevice.DATA["data"+strconv.Itoa(3)]=rand.Intn(2)
|
demoDevice.DATA["data"+strconv.Itoa(3)] = rand.Intn(2)
|
||||||
|
|
||||||
demoDevice.DATA["data"+strconv.Itoa(7)]=data_int[0]
|
demoDevice.DATA["data"+strconv.Itoa(7)] = data_int[0]
|
||||||
demoDevice.DATA["data"+strconv.Itoa(8)]=rand.Intn(2)
|
demoDevice.DATA["data"+strconv.Itoa(8)] = rand.Intn(2)
|
||||||
device.DATA=demoDevice.DATA
|
device.DATA = demoDevice.DATA
|
||||||
|
|
||||||
if conf.DEBUG {
|
if conf.DEBUG {
|
||||||
log.Println(device)
|
log.Println(device)
|
||||||
|
|
Loading…
Reference in New Issue