Exemplo n.º 1
0
def test_encode_command_1():
	"Encode Command - Test 1 - GET command encoding test"
        
	byte = encode_command("GET", "ON", 5, 3)
    
	assert 'k' == byte
	assert '01101011' == bin(ord(byte))[2:].zfill(8)
Exemplo n.º 2
0
 def get_device_status(self, section_name, device_name):
     """
     This function returns the status of device
     :param section_name: Character string for the section 
     :param device_name: Character string for the device
     """
     #self.rev_section_friendly_names[section_number] = sectiom_number
     #self.rev_device_friendly_names[device_number] = device_number
     #
     #cmd = C.encode_command(command_type, status, section_number, device_number )
     
     section_number = self.get_section_number(section_name)
     device_number = self.get_device_number(section_number, device_name)
     
     cmd = encode_command('GET', 'OFF', section_number, device_number)
     self.outfile.write(cmd)
     self.outfile.flush()
     
     self.infile.seek(0,2)
     while 0 == self.infile.tell():
         self.infile.seek(0,2)
         time.sleep(1)
     
     self.infile.seek(-1,2)
     byte = self.infile.read()
     
     cmd_response = decode_command(byte)
     return cmd_response['status']
Exemplo n.º 3
0
def send_command():

    byte = encode_command()
    ser.open()
    txData = byte
    ser.write(txData)

    time.sleep(10)

    rxData = ser.read(byte)
    rxData.decode_command()

    ser.close()

    print(rxData)
Exemplo n.º 4
0
def send_command():
    
    byte = encode_command(command_type, status, section_number, device_number)
    ser.open()
    
    txData = byte;
    ser.write(txData);
    
    time.sleep(10)

    rxData = ser.read(byte);
    new_byte = decode_command(rxData)
    
    
    ser.close()
    
    return new_byte
Exemplo n.º 5
0
 def set_device_status(self, section_name, device_name, status):
     """
     This is a function that sets status of one device
     :param section_name: Character string for the section
     :param device_name: Character string for the device
     :status: Can either be 'ON' or 'OFF'
     """
     
     # What language is this? Why are there commas after the lines?
     
     #SET = 1, 
     #GET = 0, 
     #ON = 1, 
     #OFF = 0,
     
     # apart from the spelling mistakes, are we trying to set the section number or get it?
     #self.rev_section_friendly_names[section_number] = sectiom_number
     #self.rev_device_friendly_names[device_number] = device_number
     
     section_number = self.get_section_number(section_name)
     device_number = self.get_device_number(section_number, device_name)
     
     # What is C???????
     #cmd = C.encode_command(command_type, status, section_number, device_number )
     cmd = encode_command('SET', status, section_number, device_number)
     
     self.outfile.write(cmd)
     self.outfile.flush()
     
     self.infile.seek(0,2)
     while 0 == self.infile.tell():
         self.infile.seek(0,2)
         time.sleep(1)
     
     self.infile.seek(-1,2)
     byte = self.infile.read()
     
     #cmd_response = decode_command(byte)
     #print (ord(byte), ord(cmd))
     if byte == cmd:
         return True
     else:
         return False
Exemplo n.º 6
0
 def process_requests(self, infile, outfile):
     """
     This method reads the first byte form infile and after processing writes the encoded command in the outfile. 
     This method uses infinite loop that will process request after evry second.  
     
     """
     
     print("Running dummy PCB and waiting to process requests...")
     prev_byte = ''
     
     while True:
         time.sleep(1)
         #first check if file is not empty
         infile.seek(0,2)
         if 0 == infile.tell():
             continue
         
         infile.seek(-1,2)
         byte = infile.read(1)
         
         if byte==prev_byte:
             continue  #only do processing if there is a new command
         
         prev_byte = byte
         cmd = decode_command(byte)
         print("    Got command" + repr(cmd))
         
         if 'GET' == cmd['command_type']:
             status = self.get_status(cmd['section_number'], cmd['device_number'])
             new_byte = encode_command(cmd['command_type'], status, cmd['section_number'], cmd['device_number'] )
             
         elif 'SET' == cmd['command_type']:
             self.set_status(cmd['section_number'], cmd['device_number'], cmd['status'])
             new_byte = byte
         
         outfile.write(new_byte)
         outfile.flush()
Exemplo n.º 7
0
def test_encode_command_2():
	"Encode Command - Test 2 - SET command encoding test"
        
	byte = encode_command("SET", "OFF", 7, 3)
	assert '10111011' == bin(ord(byte))[2:].zfill(8)