Skip to content

Commit

Permalink
Reformat Python code and fix String Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Circuitsoft committed Oct 10, 2017
1 parent 8aba3ae commit 5a512cd
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 85 deletions.
85 changes: 50 additions & 35 deletions pyaci/aci/AciCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def __init__(self, OpCode, data=[], length = 1):
self.OpCode = OpCode
self.Data = data
if length != (len(self.Data) + 1):
logging.error("Length: %d, must be equal to the number of data items: %s plus 1 (for the opcode)",self.Len, str(self.Data))
logging.error("Length: %d, must be equal to the number of data items:" \
+ " %s plus 1 (for the opcode)", self.Len, str(self.Data))

def serialize(self):
pkt = [self.Len]
Expand All @@ -48,131 +49,145 @@ def serialize(self):
return pkt

def __repr__(self):
return str.format("I am %s and my Length is %d, OpCode is 0x%02x and Data is %s" %(self.__class__.__name__, self.Len, self.OpCode, self.Data))
return "I am %s and my Length is %d, OpCode is 0x%02x and Data is %s" % (
self.__class__.__name__, self.Len, self.OpCode, self.Data)

class AciEcho(AciCommandPkt):
OpCode = 0x02
MAX_ECHO_LENGTH = 30
def __init__(self, data=[], length=1):
if length > self.MAX_ECHO_LENGTH:
logging.error("ECHO command can have a maximum of %d byte packet size (including the opcode), not %d",MAX_ECHO_LENGTH,length)
logging.error("ECHO command can have a maximum of %d byte packet size" \
+ " (including the opcode), not %d", MAX_ECHO_LENGTH, length)
else:
super(AciEcho, self).__init__(length=length,OpCode=self.OpCode, data = data)
super(AciEcho, self).__init__(length=length, OpCode=self.OpCode, data=data)

class AciRadioReset(AciCommandPkt):
OpCode = 0x0E
Length = 1
def __init__(self):
super(AciRadioReset, self).__init__(length=self.Length,OpCode=self.OpCode)
super(AciRadioReset, self).__init__(length=self.Length, OpCode=self.OpCode)

class AciAppCommand(AciCommandPkt):
OpCode = 0x1A
MAX_CMD_LENGTH = 28
def __init__(self, data=[], length=1):
if length > self.MAX_CMD_LENGTH:
logging.error("App commands can have a maximum of %d byte packet size (including the opcode), not %d",MAX_CMD_LENGTH,length)
logging.error("App commands can have a maximum of %d byte packet size " \
+ "(including the opcode), not %d", MAX_CMD_LENGTH, length)
else:
super(AciAppCommand, self).__init__(length=length,OpCode=self.OpCode, data = data)
super(AciAppCommand, self).__init__(length=length, OpCode=self.OpCode,
data=data)

class AciInit(AciCommandPkt):
OpCode = 0x70
Length = 10
def __init__(self, access_address, min_interval, channel):
payload = valueToByteArray(access_address,4)
payload.extend(valueToByteArray(min_interval,4))
payload.extend(valueToByteArray(channel,1))
super(AciInit, self).__init__(length=self.Length,OpCode=self.OpCode, data=payload)
payload = valueToByteArray(access_address, 4)
payload.extend(valueToByteArray(min_interval, 4))
payload.extend(valueToByteArray(channel, 1))
super(AciInit, self).__init__(length=self.Length, OpCode=self.OpCode,
data=payload)

class AciValueSet(AciCommandPkt):
OpCode = 0x71
MAX_VAL_LENGTH = 26
def __init__(self, handle, data, length=3):
if length > self.MAX_VAL_LENGTH:
logging.error("VALUE_SET command can have a maximum of %d byte packet size (including the opcode), not %d",MAX_VAL_LENGTH,length)
logging.error("VALUE_SET command can have a maximum of %d byte packet size" \
+ " (including the opcode), not %d", MAX_VAL_LENGTH, length)
else:
payload = valueToByteArray(handle,2)
payload = valueToByteArray(handle, 2)
payload.extend(data)
super(AciValueSet, self).__init__(length=length, OpCode=self.OpCode, data=payload)
super(AciValueSet, self).__init__(length=length, OpCode=self.OpCode,
data=payload)

class AciValueEnable(AciCommandPkt):
OpCode = 0x72
Length = 3
def __init__(self, handle):
payload = valueToByteArray(handle,2)
super(AciValueEnable, self).__init__(length=self.Length, OpCode=self.OpCode, data=payload)
payload = valueToByteArray(handle, 2)
super(AciValueEnable, self).__init__(length=self.Length, OpCode=self.OpCode,
data=payload)

class AciValueDisable(AciCommandPkt):
OpCode = 0x73
Length = 3
def __init__(self, handle):
payload = valueToByteArray(handle,2)
super(AciValueDisable, self).__init__(length=self.Length, OpCode=self.OpCode, data=payload)
payload = valueToByteArray(handle, 2)
super(AciValueDisable, self).__init__(length=self.Length, OpCode=self.OpCode,
data=payload)

class AciStart(AciCommandPkt):
OpCode = 0x74
Length = 1
def __init__(self):
super(AciStart, self).__init__(length=self.Length,OpCode=self.OpCode)
super(AciStart, self).__init__(length=self.Length, OpCode=self.OpCode)

class AciStop(AciCommandPkt):
OpCode = 0x75
Length = 1
def __init__(self):
super(AciStop, self).__init__(length=self.Length,OpCode=self.OpCode)
super(AciStop, self).__init__(length=self.Length, OpCode=self.OpCode)

class AciFlagSet(AciCommandPkt):
OpCode = 0x76
Length = 5
def __init__(self, handle, flag_index, flag_value):
payload = valueToByteArray(handle,2)
payload.extend(valueToByteArray(flag_index,1))
payload.extend(valueToByteArray(flag_value,1))
super(AciFlagSet, self).__init__(length=self.Length,OpCode=self.OpCode, data=payload)
payload = valueToByteArray(handle, 2)
payload.extend(valueToByteArray(flag_index, 1))
payload.extend(valueToByteArray(flag_value, 1))
super(AciFlagSet, self).__init__(length=self.Length, OpCode=self.OpCode,
data=payload)

class AciFlagGet(AciCommandPkt):
OpCode = 0x77
Length = 4
def __init__(self, handle, flag_index):
payload = valueToByteArray(handle,2)
payload.extend(valueToByteArray(flag_index,1))
super(AciFlagGet, self).__init__(length=self.Length,OpCode=self.OpCode, data=payload)
payload = valueToByteArray(handle, 2)
payload.extend(valueToByteArray(flag_index, 1))
super(AciFlagGet, self).__init__(length=self.Length, OpCode=self.OpCode,
data=payload)

class AciDfuData(AciCommandPkt):
OpCode = 0x78
MAX_DFU_LENGTH = 26
def __init__(self, data=[], length=1):
if length > self.MAX_DFU_LENGTH:
logging.error("DFU_DATA command can have a maximum of %d byte packet size (including the opcode), not %d",MAX_DFU_LENGTH,length)
logging.error("DFU_DATA command can have a maximum of %d byte packet size " \
+ "(including the opcode), not %d", MAX_DFU_LENGTH, length)
else:
super(AciDfuData, self).__init__(length=length,OpCode=self.OpCode, data = data)
super(AciDfuData, self).__init__(length=length, OpCode=self.OpCode,
data=data)

class AciValueGet(AciCommandPkt):
OpCode = 0x7A
Length = 3
def __init__(self, handle):
payload = valueToByteArray(handle,2)
super(AciValueGet, self).__init__(length=self.Length, OpCode=self.OpCode, data=payload)
payload = valueToByteArray(handle, 2)
super(AciValueGet, self).__init__(length=self.Length, OpCode=self.OpCode,
data=payload)

class AciBuildVersionGet(AciCommandPkt):
OpCode = 0x7B
Length = 1
def __init__(self):
super(AciBuildVersionGet, self).__init__(length=self.Length,OpCode=self.OpCode)
super(AciBuildVersionGet, self).__init__(length=self.Length, OpCode=self.OpCode)

class AciAccessAddressGet(AciCommandPkt):
OpCode = 0x7C
Length = 1
def __init__(self):
super(AciAccessAddressGet, self).__init__(length=self.Length,OpCode=self.OpCode)
super(AciAccessAddressGet, self).__init__(length=self.Length, OpCode=self.OpCode)

class AciChannelGet(AciCommandPkt):
OpCode = 0x7D
Length = 1
def __init__(self):
super(AciChannelGet, self).__init__(length=self.Length,OpCode=self.OpCode)
super(AciChannelGet, self).__init__(length=self.Length, OpCode=self.OpCode)

class AciIntervalMinMsGet(AciCommandPkt):
OpCode = 0x7F
Length = 1
def __init__(self):
super(AciIntervalMinMsGet, self).__init__(length=self.Length,OpCode=self.OpCode)
super(AciIntervalMinMsGet, self).__init__(length=self.Length, OpCode=self.OpCode)
47 changes: 35 additions & 12 deletions pyaci/aci/AciEvent.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,27 @@ def __init__(self, pkt):
logging.error('Packet size must be > 1, packet contents: %s', str(pkt))

def __repr__(self):
return str.format("%s length:%d opcode:0x%02x data:%s" %(self.__class__.__name__, self.Len, self.OpCode, self.Data))
return "%s length:%d opcode:0x%02x data:%s" % (self.__class__.__name__,
self.Len, self.OpCode, self.Data)

class AciDeviceStarted(AciEventPkt):
#OpCode = 0x81
def __init__(self,pkt):
super(AciDeviceStarted, self).__init__(pkt)
if self.Len != 4:
logging.error("Invalid length for %s event: %s", self.__class__.__name__, str(pkt))
logging.error("Invalid length for %s event: %s",
self.__class__.__name__, str(pkt))
else:
self.OperatingMode = pkt[2]
self.HWError = pkt[3]
self.DataCreditAvailable = pkt[4]

def __repr__(self):
return str.format("%s length:%d opcode:0x%02x operating_mode:0x%02x hw_error:0x%02x data_credit_available:0x%02x" %(self.__class__.__name__, self.Len, self.OpCode, self.OperatingMode, self.HWError, self.DataCreditAvailable))
return "%s length:%d opcode:0x%02x operating_mode:0x%02x hw_error:0x%02x " \
+ "data_credit_available:0x%02x" % (self.__class__.__name__,
self.Len, self.OpCode,
self.OperatingMode, self.HWError,
self.DataCreditAvailable)

class AciEchoRsp(AciEventPkt):
#OpCode = 0x82
Expand All @@ -88,14 +94,18 @@ class AciCmdRsp(AciEventPkt):
def __init__(self,pkt):
super(AciCmdRsp, self).__init__(pkt)
if self.Len < 3:
logging.error("Invalid length for %s event: %s", self.__class__.__name__, str(pkt))
logging.error("Invalid length for %s event: %s",
self.__class__.__name__, str(pkt))
else:
self.CommandOpCode = pkt[2]
self.StatusCode = pkt[3]
self.Data = pkt[4:]

def __repr__(self):
return str.format("%s length:%d opcode:0x%02x command_opcode:%s status_code:%s data:%s" %(self.__class__.__name__, self.Len, self.OpCode, AciCommand.AciCommandLookUp(self.CommandOpCode), AciStatusLookUp(self.StatusCode), self.Data))
return "%s length:%d opcode:0x%02x command_opcode:%s status_code:%s data:%s" % (
self.__class__.__name__, self.Len, self.OpCode,
AciCommand.AciCommandLookUp(self.CommandOpCode),
AciStatusLookUp(self.StatusCode), self.Data)

class SensorValues(object):
STATUS_JOSTLE_FLAG = 0x01
Expand All @@ -118,16 +128,21 @@ def __init__(self, sensor_id, data):
self.data = data
def __repr__(self):
if self.is_valid:
return "SensorValues: sensor_id:{sensor_id} proximity_ids:{proximity_ids} proximity_rssi:{proximity_rssi} battery:{battery}, accel:({accel_x}, {accel_y}, {accel_z}), status:{status}, valid_time:{valid_time}".format(**vars(self))
return "SensorValues: sensor_id:{sensor_id} proximity_ids:{proximity_ids} " \
+ "proximity_rssi:{proximity_rssi} battery:{battery}, accel:(" \
+ "{accel_x}, {accel_y}, {accel_z}), status:{status}, " \
+ "valid_time:{valid_time}".format(**vars(self))
else:
return "SensorValues: invalid data from sensor {sensor_id}: ({data})".format(sensor_id=self.sensor_id, data=self.data)
return "SensorValues: invalid data from sensor {sensor_id}: ({data})"\
.format(**vars(self))

class AciEventNew(AciEventPkt):
#OpCode = 0xB3
def __init__(self,pkt):
super(AciEventNew, self).__init__(pkt)
if self.Len < 3:
logging.error("Invalid length for %s event: %s", self.__class__.__name__, str(pkt))
logging.error("Invalid length for %s event: %s",
self.__class__.__name__, str(pkt))
else:
self.ValueHandle = (pkt[3] << 8) + pkt[2]
self.VersionDelta = (pkt[5] << 8) + pkt[4]
Expand All @@ -141,9 +156,12 @@ def sensor_values(self):

def __repr__(self):
if self.is_sensor_update():
return str.format("%s(%d) %s" %(self.__class__.__name__, self.VersionDelta, self.sensor_values()))
return "%s(%d) %s" % (self.__class__.__name__, self.VersionDelta,
self.sensor_values())
else:
return str.format("%s length:%d opcode:0x%02x value_handle:0x%04x data:%s" %(self.__class__.__name__, self.Len, self.OpCode, self.ValueHandle, self.Data))
return "%s length:%d opcode:0x%02x value_handle:0x%04x data:%s" % (
self.__class__.__name__, self.Len, self.OpCode,
self.ValueHandle, self.Data)

class AciEventUpdate(AciEventNew):
#OpCode = 0xB4
Expand All @@ -164,10 +182,15 @@ class HeartbeatMsg(object):
def __init__(self, data):
if len(data) != 18:
raise ValueError("Error: expected 18 bytes, got %s" % data)
(self.rssi, self.received_at, self.received_at_ms, self.local_clock_version, self.sensor_id, self.epoch_seconds, self.epoch_ms, self.clock_version) = unpack('<BiHHBiHH', bytearray(data))
(self.rssi, self.received_at, self.received_at_ms, self.local_clock_version,
self.sensor_id, self.epoch_seconds, self.epoch_ms, self.clock_version
) = unpack('<BiHHBiHH', bytearray(data))

def __repr__(self):
return "Heartbeat: rssi:{rssi} sensor_id:{sensor_id} epoch:{epoch_seconds} ms:{epoch_ms} clock_version:{clock_version} received_at:{received_at} received_at_ms:{received_at_ms} local_clock_version:{local_clock_version}".format(**vars(self))
return "Heartbeat: rssi:{rssi} sensor_id:{sensor_id} epoch:{epoch_seconds} " \
+ "ms:{epoch_ms} clock_version:{clock_version} received_at:{received_at} " \
+ "received_at_ms:{received_at_ms} " \
+ "local_clock_version:{local_clock_version}".format(**vars(self))

class AciEventAppEvt(AciEventPkt):
APP_EVENT_OPCODE_HEARTBEAT = 0x01
Expand Down
13 changes: 9 additions & 4 deletions pyaci/aci_serial/AciUart.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ def write_aci_cmd(self, cmd):
self.WriteData(cmd.serialize())
retval = self.Wait(self)
if retval == None:
logging.info('cmd %s, timeout waiting for event' % (cmd.__class__.__name__))
logging.info('cmd %s, timeout waiting for event',
cmd.__class__.__name__)
else:
logging.error('The command provided is not valid: %s\nIt must be an instance of the AciCommandPkt class (or one of its subclasses)', str(cmd))
logging.error('The command provided is not valid: %s\n' \
+ 'It must be an instance of the AciCommandPkt class ' \
+ '(or one of its subclasses)', str(cmd))
return retval


Expand All @@ -80,7 +83,8 @@ def __init__(self, port, baudrate=115200, device_name=None, rtscts=False):

self._write_lock = threading.Lock()

logging.debug("log Opening port %s, baudrate %s, rtscts %s", port, baudrate, rtscts)
logging.debug("log Opening port %s, baudrate %s, rtscts %s",
port, baudrate, rtscts)
self.serial = Serial(port=port, baudrate=baudrate, rtscts=rtscts, timeout=0.1)

self.keep_running = True
Expand Down Expand Up @@ -135,4 +139,5 @@ def WriteData(self, data):
self.ProcessCommand(data)

def __repr__(self):
return '%s(port="%s", baudrate=%s, device_name="%s")' % (self.__class__.__name__, self.serial.port, self.serial.baudrate, self.device_name)
return '%s(port="%s", baudrate=%s, device_name="%s")' % (self.__class__.__name__,
self.serial.port, self.serial.baudrate, self.device_name)
10 changes: 7 additions & 3 deletions pyaci/configure_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ def configure_sensor(serial_device, sensor_id, serial_enabled, channel, sleep_en

if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("-d", "--device", dest="device", required=True, help="Serial device, e.g. /dev/cu.usbserial-DO00C2G2")
parser.add_argument("-d", "--device", dest="device", required=True,
help="Serial device, e.g. /dev/cu.usbserial-DO00C2G2")
parser.add_argument('id', type=int, help='the id to assign this sensor')
parser.add_argument('--no-sleeping', dest='sleep_enabled', action='store_false')
parser.set_defaults(sleep_enabled=True)
parser.add_argument('--no-serial', dest='serial_enabled', action='store_false')
parser.set_defaults(serial_enabled=True)
parser.add_argument('--channel', type=int, help='bluetooth channel of sensei network: should be 1-39 (one of 37,38,39 usually best)')
parser.add_argument('--channel', type=int,
help='bluetooth channel of sensei network: should be 1-39 ' \
+ '(one of 37,38,39 usually best)')
parser.set_defaults(channel=38)
options = parser.parse_args()
configure_sensor(options.device, options.id, options.serial_enabled, options.channel, options.sleep_enabled)
configure_sensor(options.device, options.id, options.serial_enabled,
options.channel, options.sleep_enabled)
Loading

0 comments on commit 5a512cd

Please sign in to comment.