Skip to content

Commit

Permalink
Merge pull request #74 from SEL-Columbia/0.2.4-dev
Browse files Browse the repository at this point in the history
Format fixes
  • Loading branch information
jmbott authored Dec 13, 2017
2 parents f75a354 + aa10657 commit 1830dbb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
12 changes: 6 additions & 6 deletions minigrid/device_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def write_vendor_card(session, cache, key, minigrid_id, payment_id, vendor):
int(time.time()).to_bytes(4, 'big'), # card produced time
bytes(4), # card read time, set as zeros
uuid.UUID(payment_id).bytes,
b'\x00', # Application Flag, has the card been used? Initially no
b'\x00', # Application Flag, has the card been used? Initially no
bytes(12),
))
sector_2_content = b''.join((
Expand Down Expand Up @@ -81,14 +81,14 @@ def write_customer_card(
int(time.time()).to_bytes(4, 'big'), # card produced time
bytes(4), # card read time, set as zeros
uuid.UUID(payment_id).bytes,
b'\x00', # Application Flag, has the card been used? Initially no
b'\x00', # Application Flag, has the card been used? Initially no
bytes(12),
))
sector_2_content = b''.join((
customer.customer_user_id.encode('ascii'), # 0000-9999 ASCII
uuid.UUID(minigrid_id).bytes,
int(customer.customer_current_limit).to_bytes(4, 'big'), # Current Limit mA
int(customer.customer_energy_limit).to_bytes(4, 'big'), # Energy Limit Wh
int(customer.customer_current_limit).to_bytes(4, 'big'), # Limit mA
int(customer.customer_energy_limit).to_bytes(4, 'big'), # Limit Wh
bytes(3),
))
sector_2 = b''.join((
Expand Down Expand Up @@ -135,7 +135,7 @@ def write_maintenance_card_card(
int(time.time()).to_bytes(4, 'big'), # card produced time
bytes(4), # card read time, set as zeros
uuid.UUID(payment_id).bytes,
b'\x00', # Application Flag, has the card been used?
b'\x00', # Application Flag, has the card been used?
bytes(12),
))
mc_id = maintenance_card.maintenance_card_card_id.encode('ascii')
Expand Down Expand Up @@ -200,7 +200,7 @@ def write_credit_card(
b'\x00\x00\x00\x00', # old card produce time section
bytes(4), # card read time, set as zeros
uuid.UUID(payment_id).bytes,
b'\x00', # Application Flag, has the card been used?
b'\x00', # Application Flag, has the card been used?
bytes(12),
))
credit_card_id = uuid.uuid4()
Expand Down
15 changes: 9 additions & 6 deletions minigrid/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,10 @@ def post(self, minigrid_id):
grid.customers.append(models.Customer(
customer_user_id=self.get_argument('customer_user_id'),
customer_name=self.get_argument('customer_name'),
customer_current_limit=self.get_argument('customer_current_limit'),
customer_energy_limit=self.get_argument('customer_energy_limit')))
customer_current_limit=self.get_argument(
'customer_current_limit'),
customer_energy_limit=self.get_argument(
'customer_energy_limit')))
except (IntegrityError, DataError) as error:
if 'customer_name_key' in error.orig.pgerror:
message = 'A customer with that name already exists'
Expand Down Expand Up @@ -653,7 +655,7 @@ def _user_or_maintenance_card(binary):

def _credit_card(session, cipher, binary, credit_card_id):
result = OrderedDict()
raw_sector_3 = unhexlify(binary[183:273])
# raw_sector_3 = unhexlify(binary[183:273])
# logging.info(f'Sector 3: {raw_sector_3}')
# result[3] contains tariff information
# result[3] = _decrypt(cipher, unhexlify(binary[183:273])).hex()
Expand Down Expand Up @@ -708,16 +710,17 @@ def _pack_into_dict(session, binary):
device_exists = session.query(
exists().where(models.Device.address == device_address)).scalar()
except Exception as error:
import logging # remove
import logging # remove
logging.error(str(error))
device_exists = False
if not device_exists: # TODO: new error class
raise tornado.web.HTTPError(
400, 'bad device id {}'.format(binary[:12]))
binary = binary[12:] # Remove device address form binary
binary = binary[12:] # Remove device address form binary
result = OrderedDict()
# Is it safe to assume that sector 1 is always first? I hope so
sector_1 = unhexlify(binary[1:91]) # Sector label is one character, ignore it, take 90 after as sector 1
# Sector label is one character, ignore it, take 90 after as sector 1
sector_1 = unhexlify(binary[1:91])
# logging.info(f'Sector 1: {sector_1}')
# Use this for the future... displaying in the UI
# system_id = sector_1[:2]
Expand Down
8 changes: 5 additions & 3 deletions minigrid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,19 @@ class Customer(Base):
pg.TEXT, sa.CheckConstraint("customer_user_id ~ '\d{4}'"),
nullable=False)
customer_current_limit = sa.Column(
pg.TEXT,
pg.INTEGER,
sa.CheckConstraint("customer_current_limit >= 0"),
nullable=False)
customer_energy_limit = sa.Column(
pg.TEXT,
pg.INTEGER,
sa.CheckConstraint("customer_energy_limit >= 0"),
nullable=False)

__table_args__ = (
sa.UniqueConstraint('customer_minigrid_id', 'customer_user_id'),
sa.UniqueConstraint('customer_minigrid_id', 'customer_name'))
sa.UniqueConstraint('customer_minigrid_id', 'customer_name'),
sa.UniqueConstraint('customer_minigrid_id', 'customer_current_limit'),
sa.UniqueConstraint('customer_minigrid_id', 'customer_energy_limit'))


class MaintenanceCard(Base):
Expand Down
15 changes: 10 additions & 5 deletions tests/python/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,8 @@ def setUp(self):
session.add(models.System(day_tariff=1, night_tariff=1))
self.minigrid = models.Minigrid(minigrid_name='a')
self.customer = models.Customer(
customer_name='c', customer_user_id='0000')
customer_name='c', customer_user_id='0000',
customer_current_limit=0, customer_energy_limit=0)
self.minigrid.customers.append(self.customer)
session.add(self.minigrid)
session.add(models.Device(address=bytes(6)))
Expand Down Expand Up @@ -714,7 +715,8 @@ def test_create_customer(self, get_current_user):
get_current_user.return_value = self.user
response = self.fetch(
f'/minigrids/{self.minigrid.minigrid_id}/customers?'
'action=create&customer_name=c2&customer_user_id=0001',
'action=create&customer_name=c2&customer_user_id=0001'
'&customer_current_limit=1000&customer_energy_limit=2500',
method='POST', body='')
self.assertResponseCode(response, 200)
customer = (
Expand All @@ -728,7 +730,8 @@ def test_create_duplicate_customer_name(self, get_current_user):
with ExpectLog('tornado.access', '400'):
response = self.fetch(
f'/minigrids/{self.minigrid.minigrid_id}/customers?'
'action=create&customer_name=c&customer_user_id=0001',
'action=create&customer_name=c&customer_user_id=0001'
'&customer_current_limit=1000&customer_energy_limit=2500',
method='POST', body='')
self.assertResponseCode(response, 400)
self.assertIn(
Expand All @@ -740,7 +743,8 @@ def test_create_duplicate_customer_user_id(self, get_current_user):
with ExpectLog('tornado.access', '400'):
response = self.fetch(
f'/minigrids/{self.minigrid.minigrid_id}/customers?'
'action=create&customer_name=c2&customer_user_id=0000',
'action=create&customer_name=c2&customer_user_id=0000'
'&customer_current_limit=1000&customer_energy_limit=2500',
method='POST', body='')
self.assertResponseCode(response, 400)
self.assertIn(
Expand All @@ -753,7 +757,8 @@ def test_create_missing_name(self, get_current_user):
with ExpectLog('tornado.access', '400'):
response = self.fetch(
f'/minigrids/{self.minigrid.minigrid_id}/customers?'
'action=create&customer_name=&customer_user_id=0001',
'action=create&customer_name=&customer_user_id=0001'
'&customer_current_limit=1000&customer_energy_limit=2500',
method='POST', body='')
self.assertResponseCode(response, 400)
self.assertIn('customer_customer_name_check', response.body.decode())
Expand Down

0 comments on commit 1830dbb

Please sign in to comment.