From 638d9980622c7d6fd8b8b858b62a462cf861600c Mon Sep 17 00:00:00 2001 From: Johannes_Hitzinger <johannes.hitzinger@h-da.de> Date: Fri, 13 Dec 2024 14:03:50 +0100 Subject: [PATCH] finished initial setup of classes --- exchange.py | 12 ++++++++++++ main.py | 15 ++++++++++----- offer.py | 8 ++++---- participant.py | 34 ++++++++++++++++++++++++++++++---- request.py | 10 +++++++--- shell.nix | 1 + trade.py | 12 ++++++++++-- 7 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 exchange.py diff --git a/exchange.py b/exchange.py new file mode 100644 index 0000000..7847e92 --- /dev/null +++ b/exchange.py @@ -0,0 +1,12 @@ +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 diff --git a/main.py b/main.py index 4e47fdc..6915656 100644 --- a/main.py +++ b/main.py @@ -1,22 +1,27 @@ 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) diff --git a/offer.py b/offer.py index fb4adba..2f33c05 100644 --- a/offer.py +++ b/offer.py @@ -1,4 +1,4 @@ -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) diff --git a/participant.py b/participant.py index 22e35b7..79e67fe 100644 --- a/participant.py +++ b/participant.py @@ -1,6 +1,8 @@ -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 diff --git a/request.py b/request.py index e0be93c..05cb08f 100644 --- a/request.py +++ b/request.py @@ -1,3 +1,7 @@ -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) + + diff --git a/shell.nix b/shell.nix index 6b17bf2..4f35d8c 100644 --- a/shell.nix +++ b/shell.nix @@ -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 diff --git a/trade.py b/trade.py index 4a19f8d..b822381 100644 --- a/trade.py +++ b/trade.py @@ -1,7 +1,15 @@ +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 -- GitLab