This commit is contained in:
kk 2019-03-14 20:16:27 +08:00
parent 0545149e45
commit e35305f5f7
6 changed files with 38 additions and 3 deletions

View File

@ -22,6 +22,7 @@ type P1004_6 struct {
DATA map[string]interface{} `json:"data"` DATA map[string]interface{} `json:"data"`
} }
// 发送数据
func send_data(code int, data ...Device) { func send_data(code int, data ...Device) {
log.Println("************************************************************************") log.Println("************************************************************************")
data_buf := string(build_data(code, data)) data_buf := string(build_data(code, data))
@ -30,6 +31,7 @@ func send_data(code int, data ...Device) {
log.Println("************************************************************************") log.Println("************************************************************************")
} }
// 生成对应code数据
func build_data(code int, data []Device) []byte { func build_data(code int, data []Device) []byte {
switch code { switch code {
case 1001: case 1001:
@ -101,6 +103,7 @@ func build_data(code int, data []Device) []byte {
return nil return nil
} }
// post到服务器
func post_to_server(post_data string) { func post_to_server(post_data string) {
url := conf.POST_TO_SERVER url := conf.POST_TO_SERVER
payload := strings.NewReader("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data\"\r\n\r\n" + post_data + "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--") payload := strings.NewReader("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"data\"\r\n\r\n" + post_data + "\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--")

View File

@ -75,6 +75,7 @@ type Wind struct {
var iconurl = conf.ICONURL var iconurl = conf.ICONURL
// 获取天气
func get_weather() { func get_weather() {
result, _ := http.Get("https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/113.960256,22.541454/realtime.json") result, _ := http.Get("https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/113.960256,22.541454/realtime.json")
body, _ := ioutil.ReadAll(result.Body) body, _ := ioutil.ReadAll(result.Body)

View File

@ -30,7 +30,7 @@ type Conf struct {
type Gateway_info struct { type Gateway_info struct {
ID string `json:"id"` ID string `json:"id"`
VERSION string `json:"version"` VERSION string `json:"version"`
LOCATION []float32 `json:"location"` LOCATION []float64 `json:"location"`
UNITNAME string `json:"单位"` UNITNAME string `json:"单位"`
CONFIG []Config `json:"config"` CONFIG []Config `json:"config"`
} }
@ -62,6 +62,7 @@ var DEVICEDATAS Devicedatas
var db, _ = sql.Open("sqlite3", "./db/gateway.db") var db, _ = sql.Open("sqlite3", "./db/gateway.db")
// 加载配置文件
func init() { func init() {
configfile, err := os.Open("conf/gateway.conf") configfile, err := os.Open("conf/gateway.conf")
check(err) check(err)
@ -86,6 +87,7 @@ var good, bad float64
var traffic_data Traffic var traffic_data Traffic
var satisfaction_data Satisfaction var satisfaction_data Satisfaction
// 初始化评价数据和人流量数据
func dbdata_init() { func dbdata_init() {
rows, err := db.Query("SELECT good, bad FROM satisfaction order by id DESC limit 1") rows, err := db.Query("SELECT good, bad FROM satisfaction order by id DESC limit 1")
if err != nil { if err != nil {
@ -107,6 +109,7 @@ func dbdata_init() {
} }
} }
// 定时更新评价数据和人流数据
func savedata_cron() { func savedata_cron() {
T := time.Now().Format("2006-01-02") T := time.Now().Format("2006-01-02")
stmt, err := db.Prepare("UPDATE satisfaction set good=?, bad=? ,create_time=? where id=(SELECT max(id) FROM satisfaction)") stmt, err := db.Prepare("UPDATE satisfaction set good=?, bad=? ,create_time=? where id=(SELECT max(id) FROM satisfaction)")
@ -117,6 +120,7 @@ func savedata_cron() {
stmt.Exec(&traffic_data.TRAFFIC, &T) stmt.Exec(&traffic_data.TRAFFIC, &T)
} }
// 每天0点插入一条数据
func insertdata() { func insertdata() {
T := time.Now().Format("2006-01-02") T := time.Now().Format("2006-01-02")
stmt, _ := db.Prepare(`INSERT INTO traffic (traffic, create_time) values (?, ?)`) stmt, _ := db.Prepare(`INSERT INTO traffic (traffic, create_time) values (?, ?)`)

View File

@ -12,6 +12,7 @@ import (
var router = gin.Default() var router = gin.Default()
// GW网关api路由
func gw_router() { func gw_router() {
router.Use(cors.Default()) router.Use(cors.Default())
v1 := router.Group("/api/v1") v1 := router.Group("/api/v1")
@ -64,6 +65,7 @@ type FeedBack struct {
var rl_data EventNotificationAlert var rl_data EventNotificationAlert
// 提交人流量api
func get_renliu_api(c *gin.Context) { func get_renliu_api(c *gin.Context) {
err := c.Bind(&rl_data) err := c.Bind(&rl_data)
if conf.DEBUG { if conf.DEBUG {
@ -76,6 +78,7 @@ func get_renliu_api(c *gin.Context) {
var pj_data FeedBack var pj_data FeedBack
// 提交评价api
func get_pj_api(c *gin.Context) { func get_pj_api(c *gin.Context) {
err = c.Bind(&pj_data) err = c.Bind(&pj_data)
check(err) check(err)
@ -83,6 +86,7 @@ func get_pj_api(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": pj_data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": pj_data})
} }
// 获取水表api
func sb_api(c *gin.Context) { func sb_api(c *gin.Context) {
DEVICEDATAS.RLock() DEVICEDATAS.RLock()
var data = make(map[string]uint16) var data = make(map[string]uint16)
@ -97,6 +101,7 @@ func sb_api(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data})
} }
// 获取电表api
func db_api(c *gin.Context) { func db_api(c *gin.Context) {
DEVICEDATAS.RLock() DEVICEDATAS.RLock()
var data = make(map[string]float32) var data = make(map[string]float32)
@ -111,6 +116,7 @@ func db_api(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data})
} }
// 获取空气数据api
func kq_api(c *gin.Context) { func kq_api(c *gin.Context) {
DEVICEDATAS.RLock() DEVICEDATAS.RLock()
var sum float32 var sum float32
@ -131,6 +137,7 @@ func kq_api(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data})
} }
// 获取氨气数据api
func aq_api(c *gin.Context) { func aq_api(c *gin.Context) {
DEVICEDATAS.RLock() DEVICEDATAS.RLock()
var sum uint16 var sum uint16
@ -163,10 +170,12 @@ func aq_api(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data})
} }
// 获取人流量api
func renliu_api(c *gin.Context) { func renliu_api(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": traffic_data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": traffic_data})
} }
// 获取评价api
func pj_api(c *gin.Context) { func pj_api(c *gin.Context) {
var data float64 var data float64
data = good / (good + bad) data = good / (good + bad)
@ -174,6 +183,7 @@ func pj_api(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": satisfaction_data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": satisfaction_data})
} }
// 精确到小数点后n位
func Round(f float64, n int) float64 { func Round(f float64, n int) float64 {
n10 := math.Pow10(n) n10 := math.Pow10(n)
return math.Trunc((f+0.5/n10)*n10) / n10 return math.Trunc((f+0.5/n10)*n10) / n10
@ -186,6 +196,7 @@ type Seat_data struct {
OCCUPYPOSITION uint16 `json:"occupyPosition"` OCCUPYPOSITION uint16 `json:"occupyPosition"`
} }
// 厕位占用api
func seat_api(c *gin.Context) { func seat_api(c *gin.Context) {
DEVICEDATAS.RLock() DEVICEDATAS.RLock()
type_id := c.Param("id") type_id := c.Param("id")
@ -219,6 +230,7 @@ func seat_api(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data}) c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data})
} }
// 拼凑位置信息的数据
func get_seat_device_data(id string) []string { func get_seat_device_data(id string) []string {
var data = make([]string, 2) var data = make([]string, 2)
for _, i := range conf.DEVICES_LIST { for _, i := range conf.DEVICES_LIST {
@ -232,6 +244,7 @@ func get_seat_device_data(id string) []string {
return data return data
} }
// 从配置文件中匹配对应的数据
func scan_seat_from_confdata(T string, conf_data [][]interface{}) Seat_data { func scan_seat_from_confdata(T string, conf_data [][]interface{}) Seat_data {
var data Seat_data var data Seat_data
for _, i := range conf_data { for _, i := range conf_data {
@ -253,7 +266,6 @@ func scan_seat_from_confdata(T string, conf_data [][]interface{}) Seat_data {
} }
type WEATHER struct { type WEATHER struct {
// gorm.Model
SKYCON string `json:"skycon"` SKYCON string `json:"skycon"`
ICON_URL string `json:"icon_url"` ICON_URL string `json:"icon_url"`
O3 float32 `json:"o3"` O3 float32 `json:"o3"`
@ -277,6 +289,7 @@ type WEATHER struct {
HUMIDITY_INDOOR interface{} `json:"humidity_indoor"` HUMIDITY_INDOOR interface{} `json:"humidity_indoor"`
} }
// 获取天气情况api
func gw_weather_api(c *gin.Context) { func gw_weather_api(c *gin.Context) {
DEVICEDATAS.RLock() DEVICEDATAS.RLock()
rows, _ := db.Query("SELECT skycon, icon_url, o3, co, so2, no2, temperature, humidity, pm25, pm10, cloudrate, aqi, dswrf, visibility, ultraviolet_desc, pres, comfort_desc, wind_direction, wind_speed FROM weather order by id DESC limit 1") rows, _ := db.Query("SELECT skycon, icon_url, o3, co, so2, no2, temperature, humidity, pm25, pm10, cloudrate, aqi, dswrf, visibility, ultraviolet_desc, pres, comfort_desc, wind_direction, wind_speed FROM weather order by id DESC limit 1")

View File

@ -6,6 +6,7 @@ import (
"time" "time"
) )
// 初始化串口
func serial_run() { func serial_run() {
SERIAL_PORT := make(map[string]*serial.Port) SERIAL_PORT := make(map[string]*serial.Port)
ports_arr := make([]string, 0) ports_arr := make([]string, 0)
@ -19,6 +20,7 @@ func serial_run() {
} }
port_arr = append(port_arr, ports_arr[i]) port_arr = append(port_arr, ports_arr[i])
} }
// 解析串口配置
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 {
@ -27,6 +29,7 @@ func serial_run() {
case "E": case "E":
PARITY = 'E' PARITY = 'E'
} }
// 打开串口
SERIAL_PORT[i], err = serial.OpenPort(&serial.Config{ SERIAL_PORT[i], err = serial.OpenPort(&serial.Config{
Name: conf.SERIALPORTS_LIST[i].COM, Name: conf.SERIALPORTS_LIST[i].COM,
Baud: conf.SERIALPORTS_LIST[i].BAUDS, Baud: conf.SERIALPORTS_LIST[i].BAUDS,
@ -40,6 +43,7 @@ func serial_run() {
if conf.DEBUG { if conf.DEBUG {
log.Println(SERIAL_PORT) log.Println(SERIAL_PORT)
} }
// 遍历设备对应的串口对应的端口并放到相应的run_port_device方法
for j, k := range SERIAL_PORT { for j, k := range SERIAL_PORT {
var the_same_port_devices [][]string var the_same_port_devices [][]string
for _, i := range conf.DEVICES_LIST { for _, i := range conf.DEVICES_LIST {
@ -57,6 +61,7 @@ func serial_run() {
select {} select {}
} }
// 轮询相同串口的设备放到对应的worker
func run_port_device(port *serial.Port, the_same_port_devices [][]string) { func run_port_device(port *serial.Port, the_same_port_devices [][]string) {
for { for {
for _, i := range the_same_port_devices { for _, i := range the_same_port_devices {

View File

@ -62,6 +62,7 @@ func yunhorn_kgl_l_v1(data_buf []byte) {
send_data(1005, device) send_data(1005, device)
} }
// 采集并解析水表数据
func yunhorn_sb_c_v1(s *serial.Port, device_id string, command [][]byte, num string) { func yunhorn_sb_c_v1(s *serial.Port, device_id string, command [][]byte, num string) {
_, err = s.Write([]byte(command[0])) _, err = s.Write([]byte(command[0]))
check(err) check(err)
@ -101,6 +102,7 @@ func yunhorn_sb_c_v1(s *serial.Port, device_id string, command [][]byte, num str
} }
} }
// 采集并解析洗手液数据
func yunhorn_xsy_l_v1(s *serial.Port, device_id string, command [][]byte, num string) { func yunhorn_xsy_l_v1(s *serial.Port, device_id string, command [][]byte, num string) {
_, err = s.Write([]byte(command[0])) _, err = s.Write([]byte(command[0]))
check(err) check(err)
@ -147,6 +149,7 @@ func yunhorn_xsy_l_v1(s *serial.Port, device_id string, command [][]byte, num st
} }
} }
// 采集并解析电表数据
func yunhorn_db_c_v1(s *serial.Port, device_id string, command [][]byte, num string) { func yunhorn_db_c_v1(s *serial.Port, device_id string, command [][]byte, num string) {
_, err = s.Write([]byte(command[0])) _, err = s.Write([]byte(command[0]))
check(err) check(err)
@ -218,6 +221,7 @@ func yunhorn_db_c_v1(s *serial.Port, device_id string, command [][]byte, num str
} }
} }
// 采集并解析空气数据
func yunhorn_kq_c_v1(s *serial.Port, device_id string, command [][]byte, num string) { func yunhorn_kq_c_v1(s *serial.Port, device_id string, command [][]byte, num string) {
_, err = s.Write([]byte(command[0])) _, err = s.Write([]byte(command[0]))
check(err) check(err)
@ -263,6 +267,7 @@ func yunhorn_kq_c_v1(s *serial.Port, device_id string, command [][]byte, num str
} }
} }
// 采集并解析氨气数据
func yunhorn_aq_c_v1(s *serial.Port, device_id string, command [][]byte, num string) { func yunhorn_aq_c_v1(s *serial.Port, device_id string, command [][]byte, num string) {
_, err = s.Write([]byte(command[0])) _, err = s.Write([]byte(command[0]))
check(err) check(err)
@ -302,6 +307,7 @@ func yunhorn_aq_c_v1(s *serial.Port, device_id string, command [][]byte, num str
} }
} }
// 采集并解析硫化氢数据
func yunhorn_lhq_c_v1(s *serial.Port, device_id string, command [][]byte, num string) { func yunhorn_lhq_c_v1(s *serial.Port, device_id string, command [][]byte, num string) {
_, err = s.Write([]byte(command[0])) _, err = s.Write([]byte(command[0]))
check(err) check(err)
@ -341,6 +347,7 @@ func yunhorn_lhq_c_v1(s *serial.Port, device_id string, command [][]byte, num st
} }
} }
// 采集并解析开关量数据
func yunhorn_kgl_c8_v1(s *serial.Port, device_id string, command [][]byte, num string) { func yunhorn_kgl_c8_v1(s *serial.Port, device_id string, command [][]byte, num string) {
buf := make([]byte, 128) buf := make([]byte, 128)
b_buf := make([]byte, 0) b_buf := make([]byte, 0)
@ -383,6 +390,7 @@ func yunhorn_kgl_c8_v1(s *serial.Port, device_id string, command [][]byte, num s
} }
} }
// 采集并解析开关量数据
func yunhorn_kgl_c16_v1(s *serial.Port, device_id string, command [][]byte, num string) { func yunhorn_kgl_c16_v1(s *serial.Port, device_id string, command [][]byte, num string) {
buf := make([]byte, 128) buf := make([]byte, 128)
_, err = s.Write([]byte(command[0])) _, err = s.Write([]byte(command[0]))
@ -406,7 +414,7 @@ func yunhorn_kgl_c16_v1(s *serial.Port, device_id string, command [][]byte, num
device.ONLINE = true device.ONLINE = true
device.DATA = make(map[string]interface{}) device.DATA = make(map[string]interface{})
for i, k := range b_buf { for i, k := range b_buf {
device.DATA["data_buf"+strconv.Itoa(i+1)] = k device.DATA["data"+strconv.Itoa(i+1)] = k
} }
device.Unlock() device.Unlock()
DEVICEDATAS.Lock() DEVICEDATAS.Lock()
@ -423,6 +431,7 @@ func yunhorn_kgl_c16_v1(s *serial.Port, device_id string, command [][]byte, num
} }
} }
// byte数据转换成uint16
func Bytes2Bits(data []byte) []uint16 { func Bytes2Bits(data []byte) []uint16 {
dst := make([]uint16, 0) dst := make([]uint16, 0)
for _, v := range data { for _, v := range data {