def save_all_pvi_input_register_value(): ''' save all Input Register in Register_Polling_List into database ''' read_log = [] logger.debug('=' * 20) for reg_name in Register_Polling_List: reg_data = INPUT_REGISTER.get(reg_name) if (reg_data): reg_addr = INPUT_REGISTER[reg_name][0] try: reg_value = get_register_value_by_name(reg_name) count = 1 while (reg_value is None) and (count < 3): logger.warning('read [' + reg_name + '] fail, retry ' + str(count) + ' after 5 seconds.') time.sleep(5) reg_value = get_register_value_by_name(reg_name) count += 1 if not reg_value is None: reg_data = RegData( modbus_id=t_modbus_id, pvi_name=t_pvs_name, #date = datetime.datetime.now(), address=reg_addr, value=float(reg_value), ) reg_data.save() logger.debug('saved,' + reg_name + ',' + str(reg_value)) else: logger.debug('not save,' + reg_name) read_log.append((reg_name, reg_value)) except Exception as e: logger.error('save pvi input register value error.', exc_info=True) else: logger.error('unknown register name %s in polling list!' % reg_name) logger.info('dump: ' + str(read_log))
def save_all_pvi_input_register_value(): ''' save all Input Register in Register_Polling_List into database ''' read_log = [] logger.debug('='*20) for reg_name in Register_Polling_List: reg_data = INPUT_REGISTER.get(reg_name) if (reg_data): reg_addr = INPUT_REGISTER[reg_name][0] try: reg_value = get_register_value_by_name(reg_name) count = 1 while (reg_value is None) and (count < 3): logger.warning('read [' + reg_name + '] fail, retry ' + str(count) + ' after 5 seconds.') time.sleep(5) reg_value = get_register_value_by_name(reg_name) count += 1 if not reg_value is None: reg_data = RegData(modbus_id=t_modbus_id, pvi_name=t_pvs_name, #date = datetime.datetime.now(), address = reg_addr, value = float(reg_value), ) reg_data.save() logger.debug('saved,'+reg_name+','+str(reg_value)) else: logger.debug('not save,'+reg_name) read_log.append((reg_name,reg_value)) except Exception as e: logger.error('save pvi input register value error.', exc_info=True) else: logger.error('unknown register name %s in polling list!' % reg_name) logger.info('dump: '+str(read_log))
def save_all_pvi_input_register_value(pvi_config_list): ''' pvi_config_list json data example :: [ { 'name': 'H5', 'type': 'DELTA_PRI_H5', #-> refer to pvi.PVI_TYPE_LIST 'modbus_id': 2, #-> pvi modbus address 'serial': { 'port': '/dev/ttyUSB0', #-> deprecated 'baudrate': 9600, 'bytesize': 8, 'parity': serial.PARITY_NONE, 'stopbits': 1, 'timeout': 0.1, } }, ] ''' dev_serial_list = get_serial_device_list() if len(dev_serial_list) <= 0: raise Exception('No serial device found!') dev_serial_id = dev_serial_list[0] logger.info('serial id: %s' % dev_serial_id) for pvi_config in pvi_config_list: pvi_type = pvi_config.get('type') if not pvi_type in pvi.PVI_TYPE_LIST: raise Exception('Unknown PVI Type %s' % pvi_type) pvi_name = pvi_config.get('name') modbus_id = pvi_config.get('modbus_id') if pvi_type == pvi.PVI_TYPE_DELTA_PRI_H5: logger.info('save RedData for pvi name: %s, type: %s, modbus_id: %s' % (pvi_name,pvi_type,modbus_id)) inverter = h5.DeltaPRIH5(dev_serial_id,int(modbus_id)) inverter.serial.baudrate = pvi_config.get('serial').get('baudrate') inverter.serial.bytesize = pvi_config.get('serial').get('bytesize') inverter.serial.parity = pvi_config.get('serial').get('parity') inverter.serial.stopbits = pvi_config.get('serial').get('stopbits') inverter.serial.timeout = pvi_config.get('serial').get('timeout') #inverter.debug = True retry_count = 0 reg_read_success = False MAX_RETRY_TIME = 5 while ( (retry_count < MAX_RETRY_TIME) and (reg_read_success == False) ): try: inverter.set_register_measurement_index() for reg_name in h5.Register_Polling_List: reg_addr = h5.INPUT_REGISTER.get(reg_name)[h5.REGISTER_ADDRESS_COL] reg_value = inverter.read_input_register_by_name(reg_name) reg_data = RegData(modbus_id=modbus_id, pvi_name=pvi_name, #date = datetime.datetime.now(), address = reg_addr, value = float(reg_value), ) reg_data.save() logger.info('save reg_data: %s, %s, %s' % (reg_name, reg_addr, reg_value)) reg_read_success = True except: retry_count += 1 logger.warning('inverter connect exception, try %s!' % retry_count , exc_info = True) sleep(1) logger.info('='*20)
def save_all_pvi_input_register_value(pvi_config_list): ''' pvi_config_list json data example :: [ { 'name': 'H5', 'type': 'DELTA_PRI_H5', #-> refer to pvi.PVI_TYPE_LIST 'modbus_id': 2, #-> pvi modbus address 'serial': { 'port': '/dev/ttyUSB0', #-> deprecated 'baudrate': 9600, 'bytesize': 8, 'parity': serial.PARITY_NONE, 'stopbits': 1, 'timeout': 0.1, } }, ] ''' dev_serial_list = get_serial_device_list() if len(dev_serial_list) <= 0: raise Exception('No serial device found!') dev_serial_id = dev_serial_list[0] logger.info('serial id: %s' % dev_serial_id) for pvi_config in pvi_config_list: pvi_type = pvi_config.get('type') if not pvi_type in pvi.PVI_TYPE_LIST: raise Exception('Unknown PVI Type %s' % pvi_type) pvi_name = pvi_config.get('name') modbus_id = pvi_config.get('modbus_id') if pvi_type == pvi.PVI_TYPE_DELTA_PRI_H5: logger.info( 'save RedData for pvi name: %s, type: %s, modbus_id: %s' % (pvi_name, pvi_type, modbus_id)) inverter = h5.DeltaPRIH5(dev_serial_id, int(modbus_id)) inverter.serial.baudrate = pvi_config.get('serial').get('baudrate') inverter.serial.bytesize = pvi_config.get('serial').get('bytesize') inverter.serial.parity = pvi_config.get('serial').get('parity') inverter.serial.stopbits = pvi_config.get('serial').get('stopbits') inverter.serial.timeout = pvi_config.get('serial').get('timeout') #inverter.debug = True retry_count = 0 reg_read_success = False MAX_RETRY_TIME = 5 while ((retry_count < MAX_RETRY_TIME) and (reg_read_success == False)): try: inverter.set_register_measurement_index() for reg_name in h5.Register_Polling_List: reg_addr = h5.INPUT_REGISTER.get(reg_name)[ h5.REGISTER_ADDRESS_COL] reg_value = inverter.read_input_register_by_name( reg_name) reg_data = RegData( modbus_id=modbus_id, pvi_name=pvi_name, #date = datetime.datetime.now(), address=reg_addr, value=float(reg_value), ) reg_data.save() logger.info('save reg_data: %s, %s, %s' % (reg_name, reg_addr, reg_value)) reg_read_success = True except: retry_count += 1 logger.warning('inverter connect exception, try %s!' % retry_count, exc_info=True) sleep(1) logger.info('=' * 20)