update and add static file

This commit is contained in:
fish 2019-11-19 15:08:53 +08:00
parent b11f72bc54
commit e19355ed9a
6 changed files with 334 additions and 255 deletions

View File

@ -55,10 +55,10 @@ lora_nodes:
- {"adr":"0c073f18",location_type":"changing_desk","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"} - {"adr":"0c073f18",location_type":"changing_desk","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"}
- {"adr":"0c0c1d18",location_type":"changing_desk","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"} - {"adr":"0c0c1d18",location_type":"changing_desk","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"}
- {"adr":"0c3b1518",location_type":"changing_desk","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"} - {"adr":"0c3b1518",location_type":"changing_desk","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"}
- {"adr":"0c4c4c17",location_type":"","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"} - {"adr":"0c2d1319",location_type":"","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"}
#吸顶 #吸顶
- {"adr":"0c4b5a17",location_type":"","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"} - {"adr":"f6400000",location_type":"","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"}
#门磁 #门磁
- {"adr":"0c255d17",location_type":"","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"} - {"adr":"0c255d17",location_type":"","location_code":"spao","location_data":"data12","current_data":0,"device":"yunhorn_kgl_c16_v1_1"}

2
go.mod
View File

@ -3,7 +3,7 @@ module yunhorn_gateway
go 1.12 go 1.12
require ( require (
github.com/eclipse/paho.mqtt.golang v1.2.0 // indirect github.com/eclipse/paho.mqtt.golang v1.2.0
github.com/gin-contrib/cors v1.3.0 github.com/gin-contrib/cors v1.3.0
github.com/gin-gonic/gin v1.4.0 github.com/gin-gonic/gin v1.4.0
github.com/mattn/go-sqlite3 v1.11.0 github.com/mattn/go-sqlite3 v1.11.0

View File

@ -3,19 +3,20 @@ package main
import ( import (
"database/sql" "database/sql"
// "encoding/json" // "encoding/json"
_ "github.com/mattn/go-sqlite3"
"log" "log"
"os" "os"
_ "github.com/mattn/go-sqlite3"
// "strconv" // "strconv"
"time"
"sync"
"math/rand" "math/rand"
"sync"
"time"
"k8s.io/apimachinery/pkg/util/yaml" "k8s.io/apimachinery/pkg/util/yaml"
"bytes" "bytes"
"io/ioutil" "io/ioutil"
) )
type Traffic struct { type Traffic struct {
@ -136,7 +137,6 @@ var loraNodeMap sync.Map
var db, _ = sql.Open("sqlite3", "./db/gateway.db") var db, _ = sql.Open("sqlite3", "./db/gateway.db")
type pluginConfig struct { type pluginConfig struct {
PodNodeSelectorPluginConfig map[string]string PodNodeSelectorPluginConfig map[string]string
} }

View File

@ -1,12 +1,13 @@
package main package main
import ( import (
"github.com/robfig/cron"
"log" "log"
"net" "net"
"os" "os"
"strconv" "strconv"
"time" "time"
"github.com/robfig/cron"
) )
var err error var err error

View File

@ -1,19 +1,20 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"io/ioutil" "io/ioutil"
"log" "log"
"math" "math"
"net"
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"bytes"
"net" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
) )
var router = gin.Default() var router = gin.Default()
@ -76,13 +77,13 @@ func writeCommand(c *gin.Context) {
byteAddr, ok := sensorMap.Load(addr) byteAddr, ok := sensorMap.Load(addr)
if !ok { if !ok {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "msg": "valid addr"}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "msg": "valid addr"})
return; return
} }
loraNode, ok := loraNodeMap.Load(addr) loraNode, ok := loraNodeMap.Load(addr)
if !ok { if !ok {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "msg": "valid addr"}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "msg": "valid addr"})
return; return
} }
commands = []byte{0x01} commands = []byte{0x01}
if command == "1" { if command == "1" {
@ -91,7 +92,7 @@ func writeCommand(c *gin.Context) {
commands = []byte{0x02} commands = []byte{0x02}
} else { } else {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "msg": "valid command"}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "msg": "valid command"})
return; return
} }
// strconv.Itoa // strconv.Itoa
@ -111,7 +112,7 @@ func writeCommand(c *gin.Context) {
if err != nil { if err != nil {
log.Println("write msg error!", err) log.Println("write msg error!", err)
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "msg": "fail"}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "msg": "fail"})
return; return
} }
} else { } else {
@ -237,7 +238,7 @@ func dealDatav3(data []byte,loragwip string) string{
cs2001 := data[6] cs2001 := data[6]
var R_data P1004_6 var R_data P1004_6
R_data.CODE = 1005 R_data.CODE = 1008
R_data.ID = adr R_data.ID = adr
//尿布台 //尿布台
R_data.VERSION = "sts_hps_3ir" R_data.VERSION = "sts_hps_3ir"
@ -272,7 +273,7 @@ func dealDatav3(data []byte,loragwip string) string{
readyData.WriteString("*") readyData.WriteString("*")
readyData.WriteString(tofTmp) readyData.WriteString(tofTmp)
} }
R_data.TIMESTAMP=time.Now().UnixNano() R_data.TIMESTAMP = time.Now().UnixNano() / 1e6
data_bufTmp, _ := json.Marshal(R_data) data_bufTmp, _ := json.Marshal(R_data)
data_buf := string(data_bufTmp) data_buf := string(data_bufTmp)
@ -323,7 +324,7 @@ func dealDatav3(data []byte,loragwip string) string{
newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA
newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE
if(!Contains(newLoraNode.LORA_GWS,loragwip)){ if !Contains(newLoraNode.LORA_GWS, loragwip) {
newLoraNode.LORA_GWS = append(newLoraNode.LORA_GWS, loragwip) newLoraNode.LORA_GWS = append(newLoraNode.LORA_GWS, loragwip)
} }
@ -334,12 +335,11 @@ func dealDatav3(data []byte,loragwip string) string{
changeDataV2(newLoraNode) changeDataV2(newLoraNode)
} }
if (datatype==2 && (extensionInTime2<=0 || extensionOutTime2<=0)){ if datatype == 2 && (extensionInTime2 <= 0 || extensionOutTime2 <= 0) {
newLoraNode.CURRENT_DATA = current newLoraNode.CURRENT_DATA = current
changeDataV2(newLoraNode) changeDataV2(newLoraNode)
} }
} else { } else {
// 非首次 // 非首次
if loraNode.(LoraNode).CHECK_TIME { if loraNode.(LoraNode).CHECK_TIME {
@ -387,7 +387,7 @@ func dealDatav3(data []byte,loragwip string) string{
newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE
newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA
newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE
if(!Contains(newLoraNode.LORA_GWS,loragwip)){ if !Contains(newLoraNode.LORA_GWS, loragwip) {
newLoraNode.LORA_GWS = append(newLoraNode.LORA_GWS, loragwip) newLoraNode.LORA_GWS = append(newLoraNode.LORA_GWS, loragwip)
} }
loraNodeMap.Store(adr, newLoraNode) loraNodeMap.Store(adr, newLoraNode)
@ -405,18 +405,17 @@ func dealDatav3(data []byte,loragwip string) string{
newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE newLoraNode.LOCATION_CODE = loraNode.(LoraNode).LOCATION_CODE
newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA newLoraNode.LOCATION_DATA = loraNode.(LoraNode).LOCATION_DATA
newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE newLoraNode.DEVICE = loraNode.(LoraNode).DEVICE
if(!Contains(newLoraNode.LORA_GWS,loragwip)){ if !Contains(newLoraNode.LORA_GWS, loragwip) {
newLoraNode.LORA_GWS = append(newLoraNode.LORA_GWS, loragwip) newLoraNode.LORA_GWS = append(newLoraNode.LORA_GWS, loragwip)
} }
if (datatype==2 && (extensionInTime2<=0 || extensionOutTime2<=0)){ if datatype == 2 && (extensionInTime2 <= 0 || extensionOutTime2 <= 0) {
newLoraNode.CURRENT_DATA = current newLoraNode.CURRENT_DATA = current
changeDataV2(newLoraNode) changeDataV2(newLoraNode)
} }
loraNodeMap.Store(adr, newLoraNode) loraNodeMap.Store(adr, newLoraNode)
} }
} }
@ -456,7 +455,6 @@ func dealDatav3(data []byte,loragwip string) string{
} }
buffer.WriteString(wdint2) buffer.WriteString(wdint2)
// log.Println("aqdata|wdfh",data[6]) // log.Println("aqdata|wdfh",data[6])
wddata := buffer.String() wddata := buffer.String()
@ -490,7 +488,6 @@ func dealDatav3(data []byte,loragwip string) string{
// log.Println("aqdata|nh3",nh3data) // log.Println("aqdata|nh3",nh3data)
buffer = new(bytes.Buffer) buffer = new(bytes.Buffer)
h2sint1 := strconv.Itoa(int(data[13])) h2sint1 := strconv.Itoa(int(data[13]))
buffer.WriteString(h2sint1) buffer.WriteString(h2sint1)
@ -631,10 +628,23 @@ var rl_data EventNotificationAlert
// 提交人流量api // 提交人流量api
func get_renliu_api(c *gin.Context) { func get_renliu_api(c *gin.Context) {
var rl_data_tmp EventNotificationAlert
err := c.Bind(&rl_data) err := c.Bind(&rl_data)
if conf.DEBUG { if conf.DEBUG {
log.Println(rl_data) log.Println(rl_data)
} }
err2 := c.Bind(&rl_data_tmp)
if conf.DEBUG {
log.Println("rl_data_tmp:", rl_data_tmp)
}
if 2 > 3 {
check(err2)
}
check(err) check(err)
send_data(1004) send_data(1004)
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": rl_data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": rl_data})

68
static/index.html Normal file
View File

@ -0,0 +1,68 @@
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
lang="zh-CN">
<meta http-equiv="content-type" content="txt/html; charset=utf-8" />
<head>
<script type="text/javascript"
src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<title>yunhorn-dev</title>
</head>
<body>
lorawan 数据调试
<div id="content"></div>
<!--
<button onclick="closeWebSocket()">测试添加内容</button>
-->
<script>
function closeWebSocket(){
$("#content").append("<p>测试</p>");
}
</script>
<script type="text/javascript">
var websocket = null;
//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
websocket = new WebSocket("wss://" + "smartoilets.cn/socketServer" + "/socket");
} else {
alert('当前浏览器 Not support websocket')
}
//连接发生错误的回调方法
websocket.onerror = function() {
console.log("onerror")
};
websocket.onopen = function() {
console.log("onopen")
websocket.send("{'code':131}")
console.log("sned code 131")
}
//接收到消息的回调方法
websocket.onmessage = function(event) {
// setMessageInnerHTML(event.data);
//console.log("websocket.message:",event.data)
console.log("websocket.message")
console.log(event.data)
var obj=eval("("+event.data+")");
console.log(obj.time)
$("#content").prepend("<p>"+event.data+"</p>");
//websocket.send("{'code':0111,'wcId':15}")
}
//连接关闭的回调方法
websocket.onclose = function() {
// setMessageInnerHTML("WebSocket连接关闭");
$("#content").prepend("<p>websocket已断开</p>");
console.log("onclose")
}
//监听窗口关闭事件当窗口关闭时主动去关闭websocket连接防止连接还没断开就关闭窗口server端会抛异常。
window.onbeforeunload = function() {
console.log("onbeforeunload")
}
</script>
</body>
</html>