pf hace 2 años
padre
commit
acb163a492
Se han modificado 13 ficheros con 173 adiciones y 30 borrados
  1. 6 0
      .idea/vcs.xml
  2. 79 9
      .idea/workspace.xml
  3. 1 1
      README.md
  4. 12 6
      controllers/weather.go
  5. 12 0
      logs/goiot-cronapi.log
  6. BIN
      goiot-cronapi
  7. 8 10
      main.go
  8. 12 1
      models/weather.go
  9. 1 1
      router/router.go
  10. 10 1
      services/box.go
  11. 13 1
      services/weather.go
  12. 11 0
      utils/util.go
  13. 8 0
      utils/var.go

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 79 - 9
.idea/workspace.xml

@@ -1,24 +1,71 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
+  <component name="BranchesTreeState">
+    <expand>
+      <path>
+        <item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
+        <item name="LOCAL_ROOT" type="e8cecc67:BranchNodeDescriptor" />
+      </path>
+      <path>
+        <item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
+        <item name="REMOTE_ROOT" type="e8cecc67:BranchNodeDescriptor" />
+      </path>
+      <path>
+        <item name="ROOT" type="e8cecc67:BranchNodeDescriptor" />
+        <item name="REMOTE_ROOT" type="e8cecc67:BranchNodeDescriptor" />
+        <item name="GROUP_NODE:origin" type="e8cecc67:BranchNodeDescriptor" />
+      </path>
+    </expand>
+    <select />
+  </component>
   <component name="ChangeListManager">
-    <list default="true" id="d2dc8b4c-4ed5-48e2-a519-676a1d7dbb24" name="Default Changelist" comment="" />
+    <list default="true" id="d2dc8b4c-4ed5-48e2-a519-676a1d7dbb24" name="Default Changelist" comment="1">
+      <change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/utils/var.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/controllers/weather.go" beforeDir="false" afterPath="$PROJECT_DIR$/controllers/weather.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/goiot-cronapi" beforeDir="false" afterPath="$PROJECT_DIR$/main" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/logs/goiot-cronapi.log" beforeDir="false" afterPath="$PROJECT_DIR$/logs/goiot-cronapi.log" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/models/weather.go" beforeDir="false" afterPath="$PROJECT_DIR$/models/weather.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/router/router.go" beforeDir="false" afterPath="$PROJECT_DIR$/router/router.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/services/box.go" beforeDir="false" afterPath="$PROJECT_DIR$/services/box.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/services/weather.go" beforeDir="false" afterPath="$PROJECT_DIR$/services/weather.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/utils/util.go" beforeDir="false" afterPath="$PROJECT_DIR$/utils/util.go" afterDir="false" />
+    </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
     <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
     <option name="LAST_RESOLUTION" value="IGNORE" />
   </component>
+  <component name="FileTemplateManagerImpl">
+    <option name="RECENT_TEMPLATES">
+      <list>
+        <option value="Go File" />
+      </list>
+    </option>
+  </component>
   <component name="GOROOT" path="F:\go1.16.1" />
+  <component name="Git.Settings">
+    <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
+  </component>
   <component name="ProjectId" id="2K4PmQBLpUatdNosnOrRDehsIQz" />
+  <component name="ProjectLevelVcsManager">
+    <ConfirmationsSetting value="2" id="Add" />
+  </component>
   <component name="ProjectViewState">
     <option name="hideEmptyMiddlePackages" value="true" />
     <option name="showLibraryContents" value="true" />
   </component>
   <component name="PropertiesComponent">
+    <property name="DefaultGoTemplateProperty" value="Go File" />
     <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
     <property name="WebServerToolWindowFactoryState" value="false" />
     <property name="go.import.settings.migrated" value="true" />
     <property name="go.sdk.automatically.set" value="true" />
     <property name="last_opened_file_path" value="$PROJECT_DIR$" />
+    <property name="restartRequiresConfirmation" value="false" />
   </component>
   <component name="RunManager">
     <configuration name="go build main.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
@@ -36,26 +83,49 @@
   <component name="TypeScriptGeneratedFilesManager">
     <option name="version" value="3" />
   </component>
+  <component name="Vcs.Log.Tabs.Properties">
+    <option name="TAB_STATES">
+      <map>
+        <entry key="MAIN">
+          <value>
+            <State />
+          </value>
+        </entry>
+      </map>
+    </option>
+  </component>
+  <component name="VcsManagerConfiguration">
+    <MESSAGE value="1" />
+    <option name="LAST_COMMIT_MESSAGE" value="1" />
+  </component>
   <component name="WindowStateProjectService">
     <state x="223" y="67" width="1089" height="714" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1673231198358">
       <screen x="0" y="0" width="1536" height="824" />
     </state>
     <state x="223" y="67" width="1089" height="714" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1536.824@0.0.1536.824" timestamp="1673231198358" />
-    <state width="1493" height="207" key="GridCell.Tab.0.bottom" timestamp="1673232220053">
+    <state width="1493" height="190" key="GridCell.Tab.0.bottom" timestamp="1673236056210">
+      <screen x="0" y="0" width="1536" height="824" />
+    </state>
+    <state width="1493" height="190" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1673236056210" />
+    <state width="1493" height="190" key="GridCell.Tab.0.center" timestamp="1673236056209">
+      <screen x="0" y="0" width="1536" height="824" />
+    </state>
+    <state width="1493" height="190" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1673236056209" />
+    <state width="1493" height="190" key="GridCell.Tab.0.left" timestamp="1673236056209">
       <screen x="0" y="0" width="1536" height="824" />
     </state>
-    <state width="1493" height="207" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1673232220053" />
-    <state width="1493" height="207" key="GridCell.Tab.0.center" timestamp="1673232220053">
+    <state width="1493" height="190" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1673236056209" />
+    <state width="1493" height="190" key="GridCell.Tab.0.right" timestamp="1673236056209">
       <screen x="0" y="0" width="1536" height="824" />
     </state>
-    <state width="1493" height="207" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1673232220053" />
-    <state width="1493" height="207" key="GridCell.Tab.0.left" timestamp="1673232220053">
+    <state width="1493" height="190" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1673236056209" />
+    <state x="361" y="145" key="Vcs.Push.Dialog.v2" timestamp="1673232264933">
       <screen x="0" y="0" width="1536" height="824" />
     </state>
-    <state width="1493" height="207" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1673232220053" />
-    <state width="1493" height="207" key="GridCell.Tab.0.right" timestamp="1673232220053">
+    <state x="361" y="145" key="Vcs.Push.Dialog.v2/0.0.1536.824@0.0.1536.824" timestamp="1673232264933" />
+    <state x="468" y="144" width="600" height="536" key="find.popup" timestamp="1673237080832">
       <screen x="0" y="0" width="1536" height="824" />
     </state>
-    <state width="1493" height="207" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1673232220053" />
+    <state x="468" y="144" width="600" height="536" key="find.popup/0.0.1536.824@0.0.1536.824" timestamp="1673237080832" />
   </component>
 </project>

+ 1 - 1
README.md

@@ -1 +1 @@
-cron
+SET CGO_ENABLED=0&& SET GOOS=linux&& SET GOARCH=amd64&&go build main.go

+ 12 - 6
controllers/weather.go

@@ -4,11 +4,10 @@ import (
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
 	"goiot-cronapi/common"
-	"goiot-cronapi/global/orm"
-	"goiot-cronapi/global/redis"
 	"goiot-cronapi/models"
 	"goiot-cronapi/pkg/logger"
 	"goiot-cronapi/services"
+	"goiot-cronapi/utils"
 	"time"
 )
 
@@ -22,11 +21,11 @@ func WeatherSample(c *gin.Context) {
 	var err error
 
 	// 获取所有在线的盒子列表
-	areaList := services.GetAreaList(orm.Eloquent)
+	areaList := services.GetAreaList()
 	logger.Info("采样城市数", zap.Int("NUM", len(areaList)))
 
 	for _, area := range areaList {
-		weather := services.GetWeatherFromRedis(redis.Cacher, area.CityId)
+		weather := services.GetWeatherFromRedis(area.CityId)
 		if weather.CityId <= 0 || len(weather.Temp) <= 0 || len(weather.Humidity) <= 0 {
 			go saveWeatherData(area)
 			continue
@@ -64,7 +63,7 @@ func saveWeatherData(area *models.AreaModel) {
 				weather.Area = area.Area
 			}
 			// 保存到Redis中
-			if err = weather.SaveToRedis(redis.Cacher); err != nil {
+			if err = weather.SaveToRedis(); err != nil {
 				logger.Info("更新Redis失败", zap.String("Error", err.Error()))
 			}
 			// 同时保存到数据库中
@@ -77,5 +76,12 @@ func saveWeatherData(area *models.AreaModel) {
 
 func saveWeatherToDB(w models.Weather) error {
 	sql := "INSERT INTO btk_runstat_weather(cityId, temp, humidity, pm25) VALUES(?, ?, ?, ?)"
-	return orm.Eloquent.Exec(sql, w.CityId, w.Temp, w.Humidity, w.PM25).Error
+	db, err := utils.OpenConnection()
+	if err != nil {
+
+		//common.ReturnSuccess(c, "db.err is:"+err.Error())
+		//return
+	}
+	defer db.Close()
+	return db.Exec(sql, w.CityId, w.Temp, w.Humidity, w.PM25).Error
 }

+ 12 - 0
logs/goiot-cronapi.log

@@ -129814,3 +129814,15 @@
 {"level":"info","ts":"2023-01-09 10:00:00","caller":"controllers/weather.go:26","msg":"采样城市数{NUM 11 1  <nil>}"}
 {"level":"info","ts":"2023-01-09 10:15:00","caller":"controllers/sample.go:24","msg":"采样盒子数{NUM 11 2  <nil>}"}
 {"level":"info","ts":"2023-01-09 10:15:00","caller":"controllers/weather.go:26","msg":"采样城市数{NUM 11 1  <nil>}"}
+{"level":"info","ts":"2023-01-09 10:52:28","caller":"goiot-cronapi/main.go:20","msg":"Server Start Info: 0.0.0.0:4004"}
+{"level":"info","ts":"2023-01-09 10:52:58","caller":"goiot-cronapi/main.go:20","msg":"Server Start Info: 0.0.0.0:4004"}
+{"level":"info","ts":"2023-01-09 11:02:38","caller":"goiot-cronapi/main.go:20","msg":"Server Start Info: 0.0.0.0:4004"}
+{"level":"info","ts":"2023-01-09 11:03:04","caller":"controllers/weather.go:26","msg":"采样城市数{NUM 11 0  <nil>}"}
+{"level":"info","ts":"2023-01-09 11:04:15","caller":"goiot-cronapi/main.go:20","msg":"Server Start Info: 0.0.0.0:4004"}
+{"level":"info","ts":"2023-01-09 11:04:18","caller":"controllers/weather.go:26","msg":"采样城市数{NUM 11 45  <nil>}"}
+{"level":"info","ts":"2023-01-09 11:14:23","caller":"goiot-cronapi/main.go:20","msg":"Server Start Info: 0.0.0.0:4004"}
+{"level":"info","ts":"2023-01-09 11:14:26","caller":"controllers/weather.go:25","msg":"采样城市数{NUM 11 45  <nil>}"}
+{"level":"info","ts":"2023-01-09 11:27:05","caller":"goiot-cronapi/main.go:20","msg":"Server Start Info: 0.0.0.0:4004"}
+{"level":"info","ts":"2023-01-09 11:27:09","caller":"controllers/weather.go:25","msg":"采样城市数{NUM 11 45  <nil>}"}
+{"level":"info","ts":"2023-01-09 11:39:03","caller":"goiot-cronapi/main.go:20","msg":"Server Start Info: 0.0.0.0:4004"}
+{"level":"info","ts":"2023-01-09 11:39:10","caller":"controllers/weather.go:25","msg":"采样城市数{NUM 11 45  <nil>}"}

BIN
goiot-cronapi


+ 8 - 10
main.go

@@ -3,11 +3,8 @@ package main
 import (
 	"go.uber.org/zap"
 	"goiot-cronapi/common"
-	"goiot-cronapi/global/orm"
-	"goiot-cronapi/global/redis"
 	"goiot-cronapi/pkg/logger"
 	"goiot-cronapi/router"
-	"goiot-pkg/tools"
 	"net/http"
 )
 
@@ -38,14 +35,15 @@ func init() {
 	}
 
 	// 加载Apollo配置
-	var apolloConf *tools.ApolloConfig
-	apolloConf, err = common.LoadApolloConfig()
-	if err != nil {
-		return
-	}
+	//var apolloConf *tools.ApolloConfig
+	//apolloConf, err = common.LoadApolloConfig()
+	//if err != nil {
+	//	return
+	//}
+
+	//orm.Eloquent = apolloConf.DB
 
-	orm.Eloquent = apolloConf.DB
-	redis.Cacher = apolloConf.Redis
+	//redis.Cacher = apolloConf.Redis
 
 	// 日志初始化
 	logger.Init(&logger.Config{

+ 12 - 1
models/weather.go

@@ -47,7 +47,18 @@ type Weather struct {
 	Created   string `json:"created"   gorm:"-"`               // 创建日期
 }
 
-func (w *Weather) SaveToRedis(rds *redisgo.Cacher) error {
+func (w *Weather) SaveToRedis() error {
+	//内网地址:r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com
+	//外网地址:r-bp1g868m1d89jycev0pd.redis.rds.aliyuncs.com
+	rds, err := redisgo.New(
+		redisgo.Options{
+			Addr:     "r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com:6379",
+			Password: "Meiyoumima1234",
+			Prefix:   "aiscrm_",
+		})
+	if err != nil {
+		panic(err)
+	}
 	jsonBytes, err := json.Marshal(w)
 	if err != nil {
 		return err

+ 1 - 1
router/router.go

@@ -22,7 +22,7 @@ func InitRouter() *gin.Engine {
 	// Cron
 	//g.POST("/cron/sample", controllers.Sample)
 	//g.POST("/cron/consume", controllers.Consume)
-	g.POST("/cron/weather", controllers.WeatherSample)
+	g.GET("/cron/weather", controllers.WeatherSample)
 
 	return router
 }

+ 10 - 1
services/box.go

@@ -3,6 +3,7 @@ package services
 import (
 	"github.com/jinzhu/gorm"
 	"goiot-cronapi/models"
+	"goiot-cronapi/utils"
 )
 
 // GetBoxIdList 获取所有在线的盒子列表
@@ -20,7 +21,15 @@ func GetBoxIdList(db *gorm.DB) []string {
 }
 
 // GetAreaList 获取所有在线的盒子对应的地区
-func GetAreaList(db *gorm.DB) models.AreaList {
+func GetAreaList() models.AreaList {
+	db, err := utils.OpenConnection()
+	if err != nil {
+
+		//common.ReturnSuccess(c, "db.err is:"+err.Error())
+		//return
+	}
+	defer db.Close()
+
 	var list models.AreaList
 	sql := `SELECT p.province, p.city, p.area, p.district, p.city_id FROM btk_box b
 			JOIN btk_project p ON p.project_auto_id = b.project_auto_id 

+ 13 - 1
services/weather.go

@@ -12,7 +12,19 @@ import (
 )
 
 // GetWeatherFromRedis 从Redis中获取天气信息
-func GetWeatherFromRedis(rds *redisgo.Cacher, cityId int64) models.Weather {
+func GetWeatherFromRedis(cityId int64) models.Weather {
+	//内网地址:r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com
+	//外网地址:r-bp1g868m1d89jycev0pd.redis.rds.aliyuncs.com
+	rds, err := redisgo.New(
+		redisgo.Options{
+			Addr:     "r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com:6379",
+			Password: "Meiyoumima1234",
+			Prefix:   "aiscrm_",
+		})
+	if err != nil {
+		panic(err)
+	}
+
 	var weather models.Weather
 	key := "CITY:" + strconv.FormatInt(cityId, 10)
 	exists, _ := rds.Exists(key)

+ 11 - 0
utils/util.go

@@ -1,6 +1,7 @@
 package utils
 
 import "reflect"
+import "github.com/jinzhu/gorm"
 
 const DateTimeFormat = "2006-01-02 15:04:05"
 
@@ -35,3 +36,13 @@ func GetValue(data interface{}, name string) interface{} {
 	}
 	return nil
 }
+func GetLinkSeeConnection() (db *gorm.DB, err error) {
+	//此为公网连接,供测试阶段使用,生产环境需要替换成内网地址连接:
+	//内网地址:rm-bp1rxj4r7a204ujhn.mysql.rds.aliyuncs.com
+	//外网地址:rm-bp1rxj4r7a204ujhnio.mysql.rds.aliyuncs.com
+	db, err = gorm.Open("mysql", "common_user:Meiyoumima1234@tcp(rm-bp1rxj4r7a204ujhn.mysql.rds.aliyuncs.com:3306)/linksee?charset=utf8&parseTime=True&loc=Local")
+	if err != nil {
+		//LZPrint("db.conn err is:", err.Error())
+	}
+	return db, err
+}

+ 8 - 0
utils/var.go

@@ -0,0 +1,8 @@
+package utils
+
+import "github.com/jinzhu/gorm"
+
+func OpenConnection() (db *gorm.DB, err error) {
+
+	return GetLinkSeeConnection()
+}