equipmentVerify.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package service
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "useEquipmentTimeEarlyWARNING/config"
  7. "useEquipmentTimeEarlyWARNING/databases"
  8. "useEquipmentTimeEarlyWARNING/util"
  9. )
  10. var serialNumber string
  11. func init() {
  12. serialNumber = config.Config.GetString("serialNumber")
  13. }
  14. // VerifyEquipmentUseTime 验证设备使用时间
  15. func VerifyEquipmentUseTime() {
  16. db := databases.Db
  17. //获取设备信息 sn 和 创建时间
  18. rows, err := db.Query("SELECT t_sn,t_dev_name,create_time,t__verify_time,t__calibration_time FROM Device WHERE t__state = 1 ")
  19. if err != nil {
  20. log.Panicln(err.Error())
  21. }
  22. fmt.Println("======================",time.Now())
  23. var (
  24. tSn,
  25. tDevName,
  26. createTime,
  27. t__verify_time, // 验证时间
  28. t__calibration_time string// 校准时间
  29. )
  30. for rows.Next() {
  31. nowTime := time.Now()
  32. rows.Scan(&tSn, &tDevName, &createTime, &t__verify_time, &t__calibration_time)
  33. fmt.Print(tSn)
  34. // 设备使用时间超过
  35. if createTime != "" {
  36. time, _ := time.Parse("2006-01-02 15:04:05", createTime)
  37. sub := nowTime.Sub(time)
  38. day := int(sub.Hours() / 24) // / 365
  39. fmt.Print(" 设备使用时间:", day,"天")
  40. if day == 1 * 365 {
  41. util.SendWarning(tSn, -1, 1014, tDevName, "", fmt.Sprintf("%s设备使用时间超过1年", tSn)) // 运维预警通知
  42. }else if day == 2 * 365 {
  43. util.SendWarning(tSn, -1, 1014, tDevName, "", fmt.Sprintf("%s设备使用时间超过2年", tSn)) // 运维预警通知
  44. }else if day == 3 * 365 {
  45. util.SendWarning(tSn, -1, 1014, tDevName, "", fmt.Sprintf("%s设备使用时间超过3年", tSn)) // 运维预警通知
  46. }
  47. }
  48. // 设备验证时间还剩
  49. if t__verify_time != "" {
  50. time, _ := time.Parse("2006-01-02 15:04:05", t__verify_time)
  51. sub := time.Sub(nowTime)
  52. day := int(sub.Hours() / 24) // / 365
  53. fmt.Print(" 设备验证时间还剩:", day,"天")
  54. if day == 1 * 60 {
  55. util.SendWarning(tSn, -1, 1003, tDevName, "", fmt.Sprintf("%s设备验证时间还剩%d天", tSn, int(day))) // 运维预警通知
  56. }else if day == 2 * 60 {
  57. util.SendWarning(tSn, -1, 1003, tDevName, "", fmt.Sprintf("%s设备验证时间还剩%d天", tSn, int(day))) // 运维预警通知
  58. }else if day == 3 * 60 {
  59. util.SendWarning(tSn, -1, 1003, tDevName, "", fmt.Sprintf("%s设备验证时间还剩%d天", tSn, int(day))) // 运维预警通知
  60. }
  61. }
  62. // 设备校准时间还剩
  63. if t__calibration_time != "" {
  64. time, _ := time.Parse("2006-01-02 15:04:05", t__calibration_time)
  65. sub := time.Sub(nowTime)
  66. day := int(sub.Hours() / 24) // / 365
  67. fmt.Print(" 设备校准时间还剩:", day,"天")
  68. if day == 1 * 60 {
  69. util.SendWarning(tSn, -1, 1003, tDevName, "", fmt.Sprintf("%s设备校准时间还剩%d天", tSn, int(day))) // 运维预警通知
  70. }else if day == 2 * 60 {
  71. util.SendWarning(tSn, -1, 1003, tDevName, "", fmt.Sprintf("%s设备校准时间还剩%d天", tSn, int(day))) // 运维预警通知
  72. }else if day == 3 * 60 {
  73. util.SendWarning(tSn, -1, 1003, tDevName, "", fmt.Sprintf("%s设备校准时间还剩%d天", tSn, int(day))) // 运维预警通知
  74. }
  75. }
  76. fmt.Print("\n")
  77. }
  78. }