This commit is contained in:
fish 2019-03-21 11:52:09 +08:00
parent b10aace99b
commit 1bcc46f458
6 changed files with 443 additions and 444 deletions

View File

@ -4,10 +4,10 @@ import (
"database/sql"
"encoding/json"
_ "github.com/mattn/go-sqlite3"
"os"
"time"
"strconv"
"log"
"os"
"strconv"
"time"
)
type Traffic struct {
@ -64,7 +64,6 @@ var conf Conf
var DEVICEDATAS Devicedatas
var demoDevice Device
var db, _ = sql.Open("sqlite3", "./db/gateway.db")
// 加载配置文件
@ -87,16 +86,14 @@ func init() {
}
DEVICEDATAS.Unlock()
demoDevice.ID="deb03e28-ddcb-4e5d-aa31-d870a1f0f09b"
demoDevice.VERSION="yunhorn_kgl_c8_v1"
demoDevice.ID = "deb03e28-ddcb-4e5d-aa31-d870a1f0f09b"
demoDevice.VERSION = "yunhorn_kgl_c8_v1"
demoDevice.DATA = make(map[string]interface{})
for a:=0;a<8;a++ {
demoDevice.DATA["data"+strconv.Itoa(a+1)]=0
for a := 0; a < 8; a++ {
demoDevice.DATA["data"+strconv.Itoa(a+1)] = 0
}
demoDevice.ONLINE=true
log.Println("init demoDevice success:",demoDevice)
demoDevice.ONLINE = true
log.Println("init demoDevice success:", demoDevice)
dbdata_init()
@ -119,7 +116,7 @@ func dbdata_init() {
rows.Close()
}
//在原本没有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 ")
if err != nil {
traffic_data.TRAFFIC = 0

View File

@ -3,8 +3,8 @@ package main
import (
"github.com/robfig/cron"
"log"
"os"
"net"
"os"
)
var err error
@ -18,7 +18,7 @@ func main() {
send_data(1001)
})
// devices := []Device{}
c.AddFunc("*/10, *, *, *, *, *", func(){
c.AddFunc("*/10, *, *, *, *, *", func() {
// log.Println(string(build_data(1001,devices)))
})
@ -43,7 +43,7 @@ func main() {
go handleConn(conn)
}
// select {}
// select {}
}

View File

@ -205,7 +205,7 @@ func seat_api(c *gin.Context) {
for _, i := range conf.GATEWAY_INFO.CONFIG {
arr := get_seat_device_data(i.ID)
version, num := arr[0], arr[1]
log.Println("conf.data:",i.DATA)
log.Println("conf.data:", i.DATA)
for k, j := range i.DATA {
if DEVICEDATAS.DATA[version+"_"+num].DATA != nil {
if DEVICEDATAS.DATA[version+"_"+num].DATA["data"+strconv.Itoa(k+1)] != nil {

View File

@ -21,7 +21,7 @@ func serial_run() {
port_arr = append(port_arr, ports_arr[i])
}
// 解析串口配置
if conf.OPENSERIAL{
if conf.OPENSERIAL {
for _, i := range port_arr {
var PARITY serial.Parity
switch conf.SERIALPORTS_LIST[i].PARITY {

View File

@ -11,7 +11,6 @@ import (
// "go-study/socket/config"
)
func handleConn(conn net.Conn) {
defer conn.Close()
@ -37,12 +36,12 @@ func handleConn(conn net.Conn) {
break
}
case <-time.After(5000 * time.Millisecond):
n,err := conn.Write(heartbeat)
if err != nil{
log.Println("write msg error!",err)
n, err := conn.Write(heartbeat)
if err != nil {
log.Println("write msg error!", err)
stopChan <- true
}else{
log.Println("write msg success!",n)
} else {
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)
case <-time.After(50 * time.Millisecond): //上面的ch如果一直没数据会阻塞那么select也会检测其他case条件检测到后3秒超时
// log.Println("超时")
if len(Rdata) >3{
if len(Rdata) > 3 {
readChan <- Rdata
Rdata = make([]byte,0)
Rdata = make([]byte, 0)
}
//strData := string(Rdata)
//Rdata = make([]byte,0)
@ -84,7 +83,7 @@ func readConn(conn net.Conn, readChan chan<- []byte, stopChan chan<- bool) {
}
log.Println(DEVICEDATAS)
log.Println("read.data:",Bytes2Bits(data[3:4]))
log.Println("read.data:", Bytes2Bits(data[3:4]))
data_buf <- data
}

View File

@ -4,9 +4,9 @@ import (
"encoding/binary"
"github.com/tarm/serial"
"log"
"math/rand"
"strconv"
"sync"
"math/rand"
)
type Devicedatas struct {
@ -14,7 +14,6 @@ type Devicedatas struct {
DATA map[string]Device
}
type Device struct {
sync.RWMutex
ID string `json:"id"`
@ -23,7 +22,7 @@ type Device struct {
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 {
// 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("ys_l_v1_data:",data_buf)
// log.Println("ys_l_v1_data:",data_buf)
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
device.ID=demoDevice.ID
device.VERSION=demoDevice.VERSION
device.ONLINE=demoDevice.ONLINE
device.ID = demoDevice.ID
device.VERSION = demoDevice.VERSION
device.ONLINE = demoDevice.ONLINE
demoDevice.DATA["data"+strconv.Itoa(1)]=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(1)] = 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(7)]=data_int[0]
demoDevice.DATA["data"+strconv.Itoa(8)]=rand.Intn(2)
device.DATA=demoDevice.DATA
demoDevice.DATA["data"+strconv.Itoa(7)] = data_int[0]
demoDevice.DATA["data"+strconv.Itoa(8)] = rand.Intn(2)
device.DATA = demoDevice.DATA
if conf.DEBUG {
log.Println(device)