Skip to content

Commit

Permalink
Merge pull request #49 from SEL-Columbia/new_card_format
Browse files Browse the repository at this point in the history
New version
  • Loading branch information
vr2262 authored Jun 23, 2017
2 parents 0c33ffd + acd3e02 commit 840d8e8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
30 changes: 18 additions & 12 deletions minigrid/device_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend

from sqlalchemy.dialects.postgresql import insert

import minigrid.models as models


Expand Down Expand Up @@ -221,18 +223,22 @@ def write_credit_card(
naive_payload[94:96],
bytes(13),
(sum(naive_payload[94:96]) & 0xFF).to_bytes(1, 'big'),
bytes(32), # maybe more?
))
cache.set('device_info', _wrap_binary(actual_payload), 5)
data = {
'credit_card_id': str(credit_card_id),
'credit_minigrid_id': minigrid_id,
'credit_amount': credit_amount,
'credit_day_tariff': day_tariff,
'credit_day_tariff_start': day_tariff_start,
'credit_night_tariff': night_tariff,
'credit_night_tariff_start': night_tariff_start,
'credit_tariff_creation_timestamp': tariff_creation_timestamp,
'credit_tariff_activation_timestamp': tariff_activation_timestamp,
}
statement = (
insert(models.CreditCardHistory)
.values(**data)
.on_conflict_do_nothing())
with models.transaction(session) as tx_session:
tx_session.add(models.CreditCardHistory(
credit_card_id=credit_card_id,
credit_minigrid_id=minigrid_id,
credit_amount=credit_amount,
credit_day_tariff=day_tariff,
credit_day_tariff_start=day_tariff_start,
credit_night_tariff=night_tariff,
credit_night_tariff_start=night_tariff_start,
credit_tariff_creation_timestamp=tariff_creation_timestamp,
credit_tariff_activation_timestamp=tariff_activation_timestamp,
))
tx_session.execute(statement)
7 changes: 5 additions & 2 deletions minigrid/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,10 @@ def _user_or_maintenance_card(binary):
def _credit_card(session, cipher, binary, credit_card_id):
result = OrderedDict()
#result[3] = _decrypt(cipher, unhexlify(binary[131:195])).hex() # contains tariff information
sector_4 = unhexlify(binary[196:]).rstrip(b'\x00')
raw_sector_4 = unhexlify(binary[196:])
if not any(raw_sector_4):
return result
sector_4 = raw_sector_4.split(b'###')[0][:-2]
# If card has been used...
record_timestamp = datetime.fromtimestamp(
int(sector_4[4:14].decode('ascii'))).isoformat()
Expand Down Expand Up @@ -762,7 +765,7 @@ def post(self):
# TODO: clean this up
payload = _pack_into_dict(self.session, body)
except Exception as error:
cache.set('card_read_error', str(error))
cache.set('card_read_error', str(error), 5)
else:
cache.set('received_info', payload, 5)
cache.delete('card_read_error')
Expand Down
2 changes: 1 addition & 1 deletion minigrid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class SystemHistory(Base):
sa.CheckConstraint('sh_meter_id > 0'), nullable=False)
sh_meter_energy_usage = sa.Column(
pg.INTEGER,
sa.CheckConstraint('sh_meter_energy_usage > 0'), nullable=False)
sa.CheckConstraint('sh_meter_energy_usage >= 0'), nullable=False)
sh_meter_credit = sa.Column(
pg.INTEGER,
sa.CheckConstraint('sh_meter_credit >= 0'), nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion prod/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '2'
services:
minigrid:
image: selcolumbia/minigrid-server:0.1.8
image: selcolumbia/minigrid-server:0.1.9
command: ./prod/run.sh --db_host=db --redis_url=redis://redis:6379/0 --minigrid-website-url=https://www.example.com
depends_on:
- redis
Expand Down
6 changes: 3 additions & 3 deletions prod/install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh
# Minigrid Server installer for version 0.1.8
# Minigrid Server installer for version 0.1.9
set -e

# Do you have docker installed?
Expand Down Expand Up @@ -108,8 +108,8 @@ $SUDO openssl dhparam -out /etc/letsencrypt/live/$LETSENCRYPT_DIR/dhparam.pem 20
printf "========================================\n"
printf " Generating configuration \n"
printf "========================================\n"
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.8/prod/docker-compose.yml > docker-compose.yml
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.8/prod/nginx.conf > nginx.conf
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.9/prod/docker-compose.yml > docker-compose.yml
$CURL -L https://raw.githubusercontent.com/SEL-Columbia/minigrid-server/0.1.9/prod/nginx.conf > nginx.conf

sed -i s/www.example.com/$LETSENCRYPT_DIR/g docker-compose.yml
sed -i s/www.example.com/$LETSENCRYPT_DIR/g nginx.conf
Expand Down

0 comments on commit 840d8e8

Please sign in to comment.