diff --git a/minigrid/device_interface.py b/minigrid/device_interface.py index 6d256b0..9d97d9d 100644 --- a/minigrid/device_interface.py +++ b/minigrid/device_interface.py @@ -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 @@ -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) diff --git a/minigrid/handlers.py b/minigrid/handlers.py index d30ba9f..79baa6b 100644 --- a/minigrid/handlers.py +++ b/minigrid/handlers.py @@ -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() @@ -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') diff --git a/minigrid/models.py b/minigrid/models.py index 18c2771..4f0dd71 100644 --- a/minigrid/models.py +++ b/minigrid/models.py @@ -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) diff --git a/prod/docker-compose.yml b/prod/docker-compose.yml index b3c181b..b33b98f 100644 --- a/prod/docker-compose.yml +++ b/prod/docker-compose.yml @@ -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 diff --git a/prod/install.sh b/prod/install.sh index 7eb0909..e2f8442 100755 --- a/prod/install.sh +++ b/prod/install.sh @@ -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? @@ -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