123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package controller
- import (
- "bytes"
- "encoding/json"
- "github.com/robfig/cron"
- "io/ioutil"
- "iot_manager_service/app/data/model"
- "iot_manager_service/app/data/service"
- device "iot_manager_service/app/device/service"
- system "iot_manager_service/app/system/dao"
- warnService "iot_manager_service/app/warn/service"
- "iot_manager_service/util/cache"
- "iot_manager_service/util/logger"
- "net/http"
- "time"
- )
- func CalcTask() {
- hourSpec := "0 0 */1 * * ?"
- //minuteSpec := "0 */15 * * * ?"
- daySpec := "0 1 0 * * ?"
- c := cron.New()
- _ = c.AddFunc(hourSpec, func() {
- devices := device.GarbageService.GetAll()
- var deviceIds []string
- for _, d := range devices {
- deviceIds = append(deviceIds, d.DeviceIMei)
- }
- GarbageDataSync(deviceIds)
- })
- _ = c.AddFunc(hourSpec, func() {
- service.EnvironmentDataService.DataSync()
- })
- _ = c.AddFunc(daySpec, func() {
- service.EnvironmentDataService.DayDataSync()
- })
- //处理告警消息
- _ = c.AddFunc("0 * */1 * * ?", func() {
- //抓取数据
- tenant := system.Tenant{}
- tenants, _ := tenant.GetTenants(0, 1000)
- for _, t := range tenants {
- warnService.PlatformAlarmService.Refresh(int(t.TenantId))
- }
- //发送告警
- warnService.TaskWarnService.HandlingAlarms()
- })
- c.Start()
- }
- func GarbageDataSync(deviceIds []string) {
- if len(deviceIds) == 0 {
- return
- }
- url := "http://124.239.149.63:5000/admin/garbage/can/getGarbageCanByDeviceIdsList"
- req := model.GarbageDataReq{
- DeviceIds: deviceIds,
- }
- data, err := json.Marshal(&req)
- if err != nil {
- logger.Logger.Warningf("GarbageDataSync Marshal err = %s", err.Error())
- return
- }
- body := bytes.NewBuffer(data)
- request, err := http.NewRequest("POST", url, body)
- if err != nil {
- logger.Logger.Warningf("GarbageDataSync NewRequest err = %s", err.Error())
- return
- }
- request.Header.Set("Authorization", "Bearer "+GarbageLogin())
- request.Header.Set("Content-Type", "application/json")
- response, err := http.DefaultClient.Do(request)
- if err != nil {
- logger.Logger.Warningf("GarbageDataSync Do err = %s", err.Error())
- return
- }
- rspData, err := ioutil.ReadAll(response.Body)
- if err != nil {
- logger.Logger.Warningf("GarbageDataSync ReadAll err = %s", err.Error())
- return
- }
- var rsp model.GarbageDataRsp
- err = json.Unmarshal(rspData, &rsp)
- if err != nil {
- logger.Logger.Warningf("GarbageDataSync Unmarshal err = %s", err.Error())
- return
- }
- if rsp.Code != 0 {
- logger.Logger.Warningf("GarbageDataSync rsp.Code != 0 msg = %s", rsp.Msg)
- return
- }
- service.GarbageDataService.BatchSave(rsp.Data)
- }
- func GarbageLogin() string {
- value, err := cache.Redis.Get(model.GarbageLoginKey).Result()
- if err == nil {
- return value
- }
- url := "http://124.239.149.63:5000/admin/user/login"
- req := model.GarbageLoginReq{
- Username: "湖南溢满",
- Password: "T2rMLUCMaaBXfjBjZ7myOA==",
- }
- data, err := json.Marshal(&req)
- if err != nil {
- logger.Logger.Warningf("GarbageLogin Marshal err = %s", err.Error())
- return ""
- }
- body := bytes.NewBuffer(data)
- request, err := http.NewRequest("POST", url, body)
- if err != nil {
- logger.Logger.Warningf("GarbageLogin NewRequest err = %s", err.Error())
- return ""
- }
- request.Header.Set("Content-Type", "application/json")
- response, err := http.DefaultClient.Do(request)
- if err != nil {
- logger.Logger.Warningf("GarbageLogin Do err = %s", err.Error())
- return ""
- }
- rspData, err := ioutil.ReadAll(response.Body)
- if err != nil {
- logger.Logger.Warningf("GarbageLogin ReadAll err = %s", err.Error())
- return ""
- }
- var rsp model.GarbageLoginRsp
- err = json.Unmarshal(rspData, &rsp)
- if err != nil {
- logger.Logger.Warningf("GarbageLogin Unmarshal err = %s", err.Error())
- return ""
- }
- if rsp.Code != 0 {
- logger.Logger.Warningf("GarbageLogin rsp.Code != 0 msg = %s", rsp.Msg)
- return ""
- }
- cache.Redis.Set(model.GarbageLoginKey, rsp.Data.AccessToken, 2*time.Hour)
- return rsp.Data.AccessToken
- }
|