123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package timetask
- import (
- "context"
- "lift-monitor/consts"
- "lift-monitor/pb"
- "lift-monitor/pb/v1"
- "lift-monitor/util"
- "encoding/json"
- "fmt"
- "sync"
- "time"
- "github.com/jaryhe/gopkgs/logger"
- "go.uber.org/zap"
- )
- var status map[string]int64
- var mutex sync.Mutex
- func handleOfflineAlarm(sn string) error {
- deviceInfo, err := util.GetDeviceInfo(sn)
- if err != nil {
- return err
- }
- alram := &v1.AlarmAddRequest{
- Date: time.Now().Format(consts.TimeSecondLayOut),
- ProjectId: deviceInfo.ProjectId,
- Sn: sn,
- DeviceName: deviceInfo.Name,
- DeviceType: consts.LiftDeviceName,
- DeviceCode: consts.LiftDeviceCode,
- }
- alram.AlarmReason = "离线"
- alram.AlarmCode = consts.DeviceOfflineCode
- // 添加告警
- _, err = pb.SmartAlarm.AlarmAdd(context.Background(), alram)
- if err != nil {
- reqByte, _ := json.Marshal(*alram)
- logger.Error("rpc",
- zap.String("call", "pb.SmartAlarm.AlarmAdd"),
- zap.String("args", string(reqByte)),
- zap.String("error", err.Error()))
- return err
- }
- return nil
- }
- func setLiftStatus(sn string, status int32) error {
- if status == consts.DeviceOffline {
- handleOfflineAlarm(sn)
- }
- rpcReq := &v1.DeviceStatusUpdateRequest{Sn: sn, Status: status, DeviceCode: consts.LiftDeviceCode}
- _, err := pb.SmartAuth.DeviceStatusUpdate(context.Background(), rpcReq)
- if err != nil {
- reqByte, _ := json.Marshal(*rpcReq)
- logger.Error("rpc",
- zap.String("call", "pb.SmartAuth.DeviceStatusUpdate"),
- zap.String("args", string(reqByte)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func liftStatusTaskImpl() {
- defer func() {
- if r := recover(); r != nil {
- err := fmt.Errorf("%+v", r)
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }()
- timeNow := time.Now().Unix()
- for k, v := range status {
- // 超过3分钟没有连接认为超时
- if (timeNow - v) > consts.MinSecond*3 {
- mutex.Lock()
- // 设置状态成功后清除缓存
- err := setLiftStatus(k, consts.DeviceOffline)
- if err == nil {
- delete(status, k)
- }
- mutex.Unlock()
- }
- }
- }
- func liftStatusTask() {
- status = make(map[string]int64)
- for {
- // 任务实现
- liftStatusTaskImpl()
- // 休眠60s
- time.Sleep(consts.MinSecond * time.Second)
- }
- }
- func UpdateLiftTime(sn string) {
- timeNow := time.Now().Unix()
- mutex.Lock()
- defer mutex.Unlock()
- // 重新启动或设备上线
- if _, ok := status[sn]; !ok {
- // 设置为在线
- err := setLiftStatus(sn, consts.DeviceOnline)
- if err != nil {
- // 设置失败不更新本地缓存
- return
- }
- }
- // 更新时间为当前时间搓
- status[sn] = timeNow
- }
|