diff --git a/do_send_data.go b/do_send_data.go index f8eae47..7d1e641 100644 --- a/do_send_data.go +++ b/do_send_data.go @@ -22,6 +22,7 @@ type P1004_6 struct { DATA map[string]interface{} `json:"data"` } +// 发送数据 func send_data(code int, data ...Device) { log.Println("************************************************************************") data_buf := string(build_data(code, data)) @@ -30,6 +31,7 @@ func send_data(code int, data ...Device) { log.Println("************************************************************************") } +// 生成对应code数据 func build_data(code int, data []Device) []byte { switch code { case 1001: @@ -101,6 +103,7 @@ func build_data(code int, data []Device) []byte { return nil } +// post到服务器 func post_to_server(post_data string) { 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--") diff --git a/get_weather.go b/get_weather.go index ee684e8..55295d2 100644 --- a/get_weather.go +++ b/get_weather.go @@ -75,6 +75,7 @@ type Wind struct { var iconurl = conf.ICONURL +// 获取天气 func get_weather() { result, _ := http.Get("https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/113.960256,22.541454/realtime.json") body, _ := ioutil.ReadAll(result.Body) diff --git a/loadconfig.go b/loadconfig.go index da33072..f02aac3 100644 --- a/loadconfig.go +++ b/loadconfig.go @@ -30,7 +30,7 @@ type Conf struct { type Gateway_info struct { ID string `json:"id"` VERSION string `json:"version"` - LOCATION []float32 `json:"location"` + LOCATION []float64 `json:"location"` UNITNAME string `json:"单位"` CONFIG []Config `json:"config"` } @@ -62,6 +62,7 @@ var DEVICEDATAS Devicedatas var db, _ = sql.Open("sqlite3", "./db/gateway.db") +// 加载配置文件 func init() { configfile, err := os.Open("conf/gateway.conf") check(err) @@ -86,6 +87,7 @@ var good, bad float64 var traffic_data Traffic var satisfaction_data Satisfaction +// 初始化评价数据和人流量数据 func dbdata_init() { rows, err := db.Query("SELECT good, bad FROM satisfaction order by id DESC limit 1") if err != nil { @@ -107,6 +109,7 @@ func dbdata_init() { } } +// 定时更新评价数据和人流数据 func savedata_cron() { 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)") @@ -117,6 +120,7 @@ func savedata_cron() { stmt.Exec(&traffic_data.TRAFFIC, &T) } +// 每天0点插入一条数据 func insertdata() { T := time.Now().Format("2006-01-02") stmt, _ := db.Prepare(`INSERT INTO traffic (traffic, create_time) values (?, ?)`) diff --git a/router.go b/router.go index 6b43b85..e8c0c8d 100644 --- a/router.go +++ b/router.go @@ -12,6 +12,7 @@ import ( var router = gin.Default() +// GW网关api路由 func gw_router() { router.Use(cors.Default()) v1 := router.Group("/api/v1") @@ -64,6 +65,7 @@ type FeedBack struct { var rl_data EventNotificationAlert +// 提交人流量api func get_renliu_api(c *gin.Context) { err := c.Bind(&rl_data) if conf.DEBUG { @@ -76,6 +78,7 @@ func get_renliu_api(c *gin.Context) { var pj_data FeedBack +// 提交评价api func get_pj_api(c *gin.Context) { err = c.Bind(&pj_data) 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}) } +// 获取水表api func sb_api(c *gin.Context) { DEVICEDATAS.RLock() 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}) } +// 获取电表api func db_api(c *gin.Context) { DEVICEDATAS.RLock() 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}) } +// 获取空气数据api func kq_api(c *gin.Context) { DEVICEDATAS.RLock() var sum float32 @@ -131,6 +137,7 @@ func kq_api(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data}) } +// 获取氨气数据api func aq_api(c *gin.Context) { DEVICEDATAS.RLock() var sum uint16 @@ -163,10 +170,12 @@ func aq_api(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": data}) } +// 获取人流量api func renliu_api(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": traffic_data}) } +// 获取评价api func pj_api(c *gin.Context) { var data float64 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}) } +// 精确到小数点后n位 func Round(f float64, n int) float64 { n10 := math.Pow10(n) return math.Trunc((f+0.5/n10)*n10) / n10 @@ -186,6 +196,7 @@ type Seat_data struct { OCCUPYPOSITION uint16 `json:"occupyPosition"` } +// 厕位占用api func seat_api(c *gin.Context) { DEVICEDATAS.RLock() 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}) } +// 拼凑位置信息的数据 func get_seat_device_data(id string) []string { var data = make([]string, 2) for _, i := range conf.DEVICES_LIST { @@ -232,6 +244,7 @@ func get_seat_device_data(id string) []string { return data } +// 从配置文件中匹配对应的数据 func scan_seat_from_confdata(T string, conf_data [][]interface{}) Seat_data { var data Seat_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 { - // gorm.Model SKYCON string `json:"skycon"` ICON_URL string `json:"icon_url"` O3 float32 `json:"o3"` @@ -277,6 +289,7 @@ type WEATHER struct { HUMIDITY_INDOOR interface{} `json:"humidity_indoor"` } +// 获取天气情况api func gw_weather_api(c *gin.Context) { 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") diff --git a/serial_init.go b/serial_init.go index d56b192..06a7fbf 100644 --- a/serial_init.go +++ b/serial_init.go @@ -6,6 +6,7 @@ import ( "time" ) +// 初始化串口 func serial_run() { SERIAL_PORT := make(map[string]*serial.Port) ports_arr := make([]string, 0) @@ -19,6 +20,7 @@ func serial_run() { } port_arr = append(port_arr, ports_arr[i]) } + // 解析串口配置 for _, i := range port_arr { var PARITY serial.Parity switch conf.SERIALPORTS_LIST[i].PARITY { @@ -27,6 +29,7 @@ func serial_run() { case "E": PARITY = 'E' } + // 打开串口 SERIAL_PORT[i], err = serial.OpenPort(&serial.Config{ Name: conf.SERIALPORTS_LIST[i].COM, Baud: conf.SERIALPORTS_LIST[i].BAUDS, @@ -40,6 +43,7 @@ func serial_run() { if conf.DEBUG { log.Println(SERIAL_PORT) } + // 遍历设备对应的串口对应的端口,并放到相应的run_port_device方法 for j, k := range SERIAL_PORT { var the_same_port_devices [][]string for _, i := range conf.DEVICES_LIST { @@ -57,6 +61,7 @@ func serial_run() { select {} } +// 轮询相同串口的设备,放到对应的worker func run_port_device(port *serial.Port, the_same_port_devices [][]string) { for { for _, i := range the_same_port_devices { diff --git a/worker.go b/worker.go index 8717fd8..f744ee1 100644 --- a/worker.go +++ b/worker.go @@ -62,6 +62,7 @@ func yunhorn_kgl_l_v1(data_buf []byte) { send_data(1005, device) } +// 采集并解析水表数据 func yunhorn_sb_c_v1(s *serial.Port, device_id string, command [][]byte, num string) { _, err = s.Write([]byte(command[0])) 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) { _, err = s.Write([]byte(command[0])) 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) { _, err = s.Write([]byte(command[0])) 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) { _, err = s.Write([]byte(command[0])) 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) { _, err = s.Write([]byte(command[0])) 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) { _, err = s.Write([]byte(command[0])) 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) { buf := make([]byte, 128) 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) { buf := make([]byte, 128) _, 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.DATA = make(map[string]interface{}) 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() 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 { dst := make([]uint16, 0) for _, v := range data {