handle.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. from demeter.core import *
  3. import modbus_tk.defines as mdef
  4. import time
  5. import random
  6. import struct
  7. import serial
  8. class Handle(object):
  9. # 设置数据
  10. def set(self, master, server):
  11. master.set_timeout(6.0)
  12. server['server_time'] = int(server['server_time'])
  13. while(True):
  14. self.run(master, server)
  15. if server['server_time'] > 0:
  16. time.sleep(server['server_time'])
  17. def run(self, master, server):
  18. gateway = Demeter.service('common').list('device_gateway', search={'server_id':server['id']})
  19. if gateway:
  20. for v in gateway:
  21. if v['product_id']:
  22. self.runCode(master, v)
  23. def runCode(self, master, gateway):
  24. code = Demeter.service('common').list('setting_modbus_code', search={'product_id':gateway['product_id']})
  25. if code:
  26. for v in code:
  27. if v['register_read_address']:
  28. try:
  29. self.setValue(v, master, gateway)
  30. except:
  31. pass
  32. # 发送数据
  33. def send(self, master, server, type_info, info, value):
  34. master.set_timeout(6.0)
  35. master.set_verbose(True)
  36. code = Demeter.service('common').one('setting_modbus_code', id=info['code_id'])
  37. if not code:
  38. return ''
  39. write_address = code['register_write_address'].split("\r\n")
  40. if not info['code_index']:
  41. info['code_index'] = 0
  42. code = self.getCode(write_address[info['code_index']], code, 2)
  43. result = ''
  44. if code['register_type']:
  45. #print(value)
  46. value = int(value)
  47. master.execute(int(code['slave_id']), int(code['register_type']), int(code['register_address']), output_value=value)
  48. result = True
  49. '''
  50. read_address = code['register_read_address'].split("\r\n")
  51. if info['code_index'] not in read_address:
  52. return True
  53. code = self.getCode(read_address, code)
  54. result = master.execute(int(code['slave_id']), int(code['register_type']), int(code['register_address']), int(code['register_length']))
  55. '''
  56. gateway = Demeter.service('common').one('device_gateway', id=info['gateway_id'])
  57. self.setValue(code, master, gateway)
  58. return result
  59. def setValue(self, code, master, gateway):
  60. config = {}
  61. config['parent'] = gateway['hardware_id']
  62. address = code['register_read_address'].split("\r\n")
  63. device_type_key = code['device_type_key'].split("\r\n")
  64. exp = code['exp'].split("\r\n")
  65. j = 0
  66. for i in address:
  67. device_type = device_type_key[j]
  68. device_type = Demeter.service('common').one('setting_device_type', key=device_type)
  69. config['type'] = device_type['key']
  70. info = Demeter.service('common').one('device_info', gateway_id=gateway['id'], code_id=code['id'], code_index=j)
  71. hardware = Demeter.service('common').one('hardware_type', id=info['hardware_type'])
  72. config['method'] = hardware['key']
  73. config['child'] = info['hardware_id']
  74. code = self.getCode(i, code)
  75. code['exp'] = exp[j]
  76. value = self.getValue(code, master, info)
  77. #print(value)
  78. j = j + 1
  79. name = info['name']
  80. Demeter.service('device').save(config, value, name, device_type)
  81. def getValue(self, code, master, info):
  82. temp = master.execute(int(code['slave_id']), int(code['register_type']), int(code['register_address']), int(code['register_length']))
  83. #value = struct.unpack('>f', struct.pack('>HH', temp[0], temp[1]))
  84. #value = str(round(temp[0], 2))
  85. #value = str(temp)
  86. value = str(temp[0])
  87. if value and value != '0':
  88. if 'exp' in code and code['exp']:
  89. value = Demeter.exp(code['exp'], value)
  90. if 'exp' in info and info['exp'] and info['exp'] != '-1':
  91. value = Demeter.exp(info['exp'], value)
  92. value = str(round(float(value), 2))
  93. #Demeter.echo('value:' + str(value) + '||temp:' + str(temp) + '||size:' + str(len(temp)))
  94. #print(value)
  95. return value
  96. def getCode(self, address, code, type = 1):
  97. (code['register_type'],code['register_address'],code['register_length']) = address.split(',')
  98. code['register_type'] = int(code['register_type'])
  99. if type == 1:
  100. if code['register_type'] == 1:
  101. # 线圈
  102. code['register_type'] = mdef.READ_COILS
  103. elif code['register_type'] == 2:
  104. code['register_type'] = mdef.READ_DISCRETE_INPUTS
  105. elif code['register_type'] == 3:
  106. code['register_type'] = mdef.READ_HOLDING_REGISTERS
  107. elif code['register_type'] == 4:
  108. code['register_type'] = mdef.READ_INPUT_REGISTERS
  109. else:
  110. if code['register_type'] == 1:
  111. # 线圈
  112. code['register_type'] = mdef.WRITE_SINGLE_COIL
  113. elif code['register_type'] == 2:
  114. code['register_type'] = mdef.WRITE_MULTIPLE_COILS
  115. elif code['register_type'] == 3:
  116. code['register_type'] = mdef.WRITE_SINGLE_REGISTER
  117. elif code['register_type'] == 4:
  118. code['register_type'] = mdef.WRITE_MULTIPLE_REGISTERS
  119. return code