from proxybusdriver import ProxyBusDriver
import time
import collections
import struct
import sys,os

class Dummy_BusDriver(ProxyBusDriver):
    def __init__(self):
        ProxyBusDriver.__init__(self)
        self.name = "Loopback_ProxyBusDriver"
        self.usclockperiod = 50000
        self.rows = []
        self.index = 1
        cwd = os.path.dirname(sys.argv[0])
#        input_file = os.path.join(cwd,'input.log')
#        with open(input_file) as fp:
#            self.rows=fp.read().split('\n')
 
        
    def open_comm(self):
        self.opened = True
        return self.opened

    def close_comm(self):
        self.opened = False

    def send(self, m_id, m_data, m_err):
        print(m_id, m_data, m_err)
        return True
        
    def iteration(self):
        if not self.rows:
            return
        
        row = self.rows[self.index].split()
        words = list( int(i,16) for i in row[6:] )
        m_id = int(row[2])
        raw=bytearray()
        for w in words:
            raw +=struct.pack('H',w)
     
        self.index = 1 if self.index == len(self.rows) - 1 else self.index + 1 
        #print(m_id,self.index)
        #if m_id == 1:
        #    self.forward_msg(m_id,raw)
        self.forward_msg(m_id,raw)
  
me = Dummy_BusDriver()
