powerManage.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. module(...,package.seeall)
  2. require "pins"
  3. local BAT_ADC_ID,DC_ADC_ID = 2,3
  4. power24Flag = nil
  5. local function powerInterrupt( msg )
  6. -- body
  7. log.info("msg:", msg)
  8. if msg==cpu.INT_GPIO_NEGEDGE then
  9. log.info("gpio11 INT NEGEDGE msg")
  10. sys.publish("24V_POWER_DOWN")
  11. modbusTT.checkNetWorkHealth("power_24_off")
  12. else
  13. log.info("gpio11 INT POSGEDGE msg")
  14. end
  15. end
  16. setGpio11Fnc = pins.setup(pio.P0_11, powerInterrupt )
  17. function closePDs( )
  18. -- body
  19. uart.close(1) --关闭串口1
  20. -- pins.setup(pio.P0_18, 1) --断电GPS
  21. pins.setup(pio.P0_13, 1) --断电485
  22. end
  23. local function setRestartClock( )
  24. local restartInterval = nvm.get("restartInterval") or 86400
  25. -- body
  26. log.info("alarm test start")
  27. local t = os.date("*t")
  28. misc.setClock({year=t.year,month=t.month,day=t.day,hour=t.hour,min=t.min,sec=t.sec})
  29. sys.wait(2000)
  30. closePDs()
  31. local onTimet = os.date("*t",os.time() + restartInterval) --下次要开机的时间为1800秒后
  32. log.info("alarm restart time", restartInterval)
  33. rtos.set_alarm(1,onTimet.year,onTimet.month,onTimet.day,onTimet.hour,onTimet.min,onTimet.sec) --设定闹铃
  34. --如果要测试关机闹钟,打开下面这2行代码
  35. sys.wait(200000)
  36. rtos.poweroff()
  37. end
  38. local function powerTaskFnc( )
  39. -- body
  40. sys.wait(4000)
  41. power24Flag = setGpio11Fnc()
  42. log.info("当前24V电平:", power24Flag)
  43. if power24Flag == 0 then
  44. -- body
  45. setRestartClock()
  46. return
  47. end
  48. pins.setup(pio.P0_13, 0) --485上电
  49. sys.waitUntil("24V_POWER_DOWN")
  50. power24Flag = setGpio11Fnc()
  51. log.info("当前24V电平:", power24Flag)
  52. setRestartClock()
  53. end
  54. adc.open(BAT_ADC_ID)
  55. function adcTimer( )
  56. -- body
  57. local adcval, voltval = adc.read(BAT_ADC_ID)
  58. log.info("bat_power.read",adcval,voltval)
  59. end
  60. sys.timerLoopStart(adcTimer, 10000)
  61. sys.taskInit(powerTaskFnc)
  62. --[[
  63. 函数名:alarMsg
  64. 功能 :开机闹钟事件的处理函数
  65. 参数 :无
  66. 返回值:无
  67. ]]
  68. local function alarMsg()
  69. print("alarMsg---------------")
  70. end
  71. --如果是关机闹钟开机,则需要软件主动重启一次,才能启动GSM协议栈
  72. if rtos.poweron_reason()==rtos.POWERON_ALARM then
  73. sys.restart("ALARM")
  74. end
  75. --注册闹钟模块
  76. rtos.init_module(rtos.MOD_ALARM)
  77. --注册闹钟消息的处理函数(如果是开机闹钟,闹钟事件到来时会调用alarmsg)
  78. rtos.on(rtos.MSG_ALARM,alarMsg)