Skip to content
Snippets Groups Projects
Commit 638d9980 authored by Johannes Hitzinger's avatar Johannes Hitzinger
Browse files

finished initial setup of classes

parent 7a0c59bf
Branches
No related tags found
No related merge requests found
from main import ureg,Q_
import locale
import time
from trade import Trade
class Exchange:
def __init__(self, p: float, ppw: float):
self.__timestamp: time.time_ns()
self.__power = Q_(p, ureg.watt)
self.__pricePerWatt = locale.currency(ppw)
self.__trades: set[Trade] = set()
self.__sig: bytes #signed exchange hash when becoming active
\ No newline at end of file
import sys
import random
import fastapi
import dilithium.dilithium
from dilithium import Dilithium
from pint import UnitRegistry
ureg = UnitRegistry()
Q_ = ureg.Quantity
v1 = Q_(5, ureg.volt)
a1 = Q_(4, ureg.amp)
p1 = v1 * a1
p1.ito(ureg.watt)
print(p1)
dis1 = 10 * ureg.watt
print(repr(dis1))
print(dis1)
d1 = Dilithium(dilithium.dilithium.DEFAULT_PARAMETERS["dilithium3"])
pk, sk = d1.keygen([random.randint(0,sys.maxsize),random.randint(0,sys.maxsize),random.randint(0,sys.maxsize),random.randint(0,sys.maxsize)])
pk, sk = d1.keygen([random.randint(0, sys.maxsize), random.randint(0, sys.maxsize), random.randint(0, sys.maxsize),
random.randint(0, sys.maxsize)])
msg = b"this is a message"
sig = d1.sign_with_input(sk,msg)
sig = d1.sign_with_input(sk, msg)
result = d1.verify(pk, msg, sig)
print(result)
from main import ureg,Q_
class Offer:
def __init__(self):
self.__power = 0
\ No newline at end of file
from exchange import Exchange
class Offer(Exchange):
def __init__(self, p: float, ppw: float):
Exchange.__init__(self, p, ppw)
from typing import Optional
from main import ureg,Q_
import locale
from random import randint
import ipaddress
import sys
from dilithium import Dilithium
......@@ -9,13 +11,37 @@ import dilithium.dilithium
from offer import Offer
from request import Request
from exchange import Exchange
from trade import Trade
import socket
def get_pub_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(('10.254.254.254', 1))
ip = s.getsockname()[0]
except Exception:
ip = '127.0.0.1'
finally:
s.close()
return ip
class Participant:
def __init__(self):
d1 = Dilithium(dilithium.dilithium.DEFAULT_PARAMETERS["dilithium3"])
self.__secretKey, self.__publicKey = d1.keygen([randint(0,sys.maxsize),randint(0,sys.maxsize),randint(0,sys.maxsize),randint(0,sys.maxsize)])
self.next_offer: Optional[Offer] = None
self.next_request: Optional[Request] = None
self.active_trades: list[Trade] = []
self.public_ip: ipaddress.IPv4Address = get_pub_ip() #set to current ipv4, confirm before each new round
self.known_participants: set[Participant] = set()
self.available_exchanges: set[Exchange] = set() #known available exchanges from other participants for next turns
self.next_exchanges: set[Exchange] = set() #own exchanges for next turn
self.active_trades: set[Trade] = set() #own active trades for this turn
self.__trade_history: list[Trade] = [] #every own past trade
self.__current_power = Q_(0, ureg.watt) #real time power exchange with the grid
self.__current_inhouse_demand = Q_(0, ureg.watt) #real time demand from household appliances and storage
self.__projected_inhouse_demand = Q_(0, ureg.watt) #expected demand for next round
self.__current_inhouse_supply = Q_(0, ureg.watt) #real time inhouse production from solar, wind, storage, ...
self.__projected_inhouse_supply = Q_(0, ureg.watt) #expected supply for next round
class Request:
def __init__(self):
self.power = 0
\ No newline at end of file
from exchange import Exchange
class Request(Exchange):
def __init__(self, p: float, ppw: float):
Exchange.__init__(self, p,ppw)
......@@ -5,6 +5,7 @@ let
mypython = pkgs.python3.withPackages (python-pkgs: [
python-pkgs.numpy
python-pkgs.pint
python-pkgs.fastapi
(python-pkgs.callPackage ./dilithium.nix { })
]);
in
......
from main import ureg,Q_
import locale
import time
from offer import Offer
from request import Request
class Trade:
def __init__(self, o: Offer, r: Request):
def __init__(self, o: Offer, r: Request, p: float, ppw: float):
self.__timestamp = time.time_ns()
self.__offer: Offer = o
self.__request: Request = r
\ No newline at end of file
self.__request: Request = r
self.__power = Q_(p, ureg.watt)
self.__pricePerWatt = locale.currency(ppw)
self.__sig: bytes
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment