# Path: config.py
# def editbatconfig(section,item,itemvalue):
# def loadconfig():


import time
import serial
import binascii
import glob
import logger
from config import config
    self.rawdat={'DataValid':False,'V00':0.0}
    self.sn=sn
    self.findbms(interfacesinuse)
    """port=self.openbms(config['files']['usbport'])
    data=self.getbmsdat(port,b'\x04\x00') # get BMS voltages
    port.close()
    self.rawdat={'V'+str(x+1):0.0 for x in range(int(len(data)/2))}
    self.rawdat['Ibat']=0.0"""

  def getdata(self):
    self.rawdat['DataValid']=False
    for i in range(5):
      try:
        self.openbms(self.bmsport)
        data=self.getbmsdat(self.port,b'\x04\x00') # get BMS Voltage
#        print(data)
        for i in range(int(len(data)/2)):
          self.rawdat['V{0:0=2}'.format(i+1)]=int.from_bytes(data[i*2:i*2+2], byteorder = 'big')/1000 \
                                              +self.rawdat['V{0:0=2}'.format(i)]
    # convert from cell voltage to total voltages
        data=self.getbmsdat(self.port,b'\x03\x00') # get other BMS data
        self.rawdat['Ibat']=int.from_bytes(data[2:4], byteorder = 'big',signed=True)
        self.rawdat['Bal']=int.from_bytes(data[12:14],byteorder = 'big',signed=False)
        for i in range(int.from_bytes(data[22:23],'big')): # read temperatures
          self.rawdat['T{0:0=1}'.format(i+1)]=(int.from_bytes(data[23+i*2:i*2+25],'big')-2731)/10
#        print (self.rawdat)
        self.rawdat['DataValid']=True
        break
      except ValueError as err:
        log.error('{}\n{}'.format(err,reply))

# Path: config.py
# def editbatconfig(section,item,itemvalue):
# def loadconfig():


# Path: bms.py
import time
import serial
import binascii
import glob
import logger
from config import config

    self.rawdat={'DataValid':False,'V00':0.0}
    self.sn=sn
    self.findbms(interfacesinuse)
    """port=self.openbms(config['files']['usbport'])
    data=self.getbmsdat(port,b'\x04\x00') # get BMS voltages
    port.close()
    self.rawdat={'V'+str(x+1):0.0 for x in range(int(len(data)/2))}
    self.rawdat['Ibat']=0.0"""

  def getdata(self):
    self.rawdat['DataValid']=False
    for i in range(5):
      try:
        self.openbms(self.bmsport)
        data=self.getbmsdat(self.port,b'\x04\x00') # get BMS Voltage
#        print(data)
        for i in range(int(len(data)/2)):
          self.rawdat['V{0:0=2}'.format(i+1)]=int.from_bytes(data[i*2:i*2+2], byteorder = 'big')/1000 \
                                              +self.rawdat['V{0:0=2}'.format(i)]
    # convert from cell voltage to total voltages
        data=self.getbmsdat(self.port,b'\x03\x00') # get other BMS data
        self.rawdat['Ibat']=int.from_bytes(data[2:4], byteorder = 'big',signed=True)
        self.rawdat['Bal']=int.from_bytes(data[12:14],byteorder = 'big',signed=False)
        for i in range(int.from_bytes(data[22:23],'big')): # read temperatures
          self.rawdat['T{0:0=1}'.format(i+1)]=(int.from_bytes(data[23+i*2:i*2+25],'big')-2731)/10
#        print (self.rawdat)
        self.rawdat['DataValid']=True
        break
      except ValueError as err:
        log.error('{}\n{}'.format(err,reply))

        time.sleep(1.0)