PowerControl.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Internal use only.
  16. This file implements controlling APC PDU via telnet.
  17. """
  18. import telnetlib
  19. class Control(object):
  20. """ control APC via telnet """
  21. @classmethod
  22. def apc_telnet_make_choice(cls, telnet, choice):
  23. """ select a choice """
  24. telnet.read_until(b'Event Log')
  25. telnet.read_until(b'>')
  26. telnet.write(choice.encode() + b'\r\n')
  27. @classmethod
  28. def apc_telnet_common_action(cls, telnet, check_str, action):
  29. """ wait until a pattern and then write a line """
  30. telnet.read_until(check_str.encode())
  31. telnet.write(action.encode() + b'\r\n')
  32. @classmethod
  33. def control(cls, apc_ip, control_dict):
  34. """
  35. control APC
  36. :param apc_ip: IP of APC
  37. :param control_dict: dict with outlet ID and "ON" or "OFF"
  38. """
  39. for _outlet in control_dict:
  40. assert 0 < _outlet < 9
  41. assert control_dict[_outlet] in ['ON', 'OFF']
  42. # telnet
  43. # set timeout as 2s so that it won't waste time even can't access APC
  44. tn = telnetlib.Telnet(host=apc_ip, timeout=5)
  45. # log on
  46. cls.apc_telnet_common_action(tn, 'User Name :', 'apc')
  47. cls.apc_telnet_common_action(tn, 'Password :', 'apc')
  48. # go to Device Manager
  49. cls.apc_telnet_make_choice(tn, '1')
  50. # go to Outlet Management
  51. cls.apc_telnet_make_choice(tn, '2')
  52. # go to Outlet Control/Configuration
  53. cls.apc_telnet_make_choice(tn, '1')
  54. # do select Outlet and control
  55. for _outlet in control_dict:
  56. # choose Outlet
  57. cls.apc_telnet_make_choice(tn, str(_outlet))
  58. # choose Control Outlet
  59. cls.apc_telnet_make_choice(tn, '1')
  60. # choose action
  61. _action = control_dict[_outlet]
  62. if 'ON' in _action:
  63. cls.apc_telnet_make_choice(tn, '1')
  64. else:
  65. cls.apc_telnet_make_choice(tn, '2')
  66. # do confirm
  67. cls.apc_telnet_common_action(tn, 'cancel :', 'YES')
  68. cls.apc_telnet_common_action(tn, 'continue...', '')
  69. # return to Outlet Control/Configuration
  70. cls.apc_telnet_make_choice(tn, '\033')
  71. cls.apc_telnet_make_choice(tn, '\033')
  72. # exit to main menu and logout
  73. tn.write(b'\033\r\n')
  74. tn.write(b'\033\r\n')
  75. tn.write(b'\033\r\n')
  76. tn.write(b'4\r\n')
  77. @classmethod
  78. def control_rest(cls, apc_ip, outlet, action):
  79. outlet_list = list(range(1, 9)) # has to be a list if we want to remove from it under Python 3
  80. outlet_list.remove(outlet)
  81. cls.control(apc_ip, dict.fromkeys(outlet_list, action))