Skip to content

Commit

Permalink
Release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorWesterlund committed Mar 29, 2021
1 parent 5a14478 commit 0f78abd
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
__pycache__

data/*
!.placeholder
75 changes: 75 additions & 0 deletions classes/Build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import json

from .Vehicle import VehicleData

# Create an echarge-configurator compatible list
class EchargeConfiguratorList:
def __init__(self):
self.output = {}

# -- Create lists --

def add_make(self,make):
if make not in self.output:
self.output[make] = {}

def add_model(self,make,model):
if make not in self.output[make]:
self.output[make][model] = {}

def add_year(self,make,model,year):
if year not in self.output[make][model]:
self.output[make][model][year] = {}

# ----

def add(self,make,model,year):
self.add_make(make)
self.add_model(make,model)
self.add_year(make,model,year)

return self.output[make][model][year]

# Save built output to file
def write(self,dest):
with open(dest,"w") as f:
json.dump(self.output,f)
return True

# Process data from vehicles_stored.json and cables.json
class Build(EchargeConfiguratorList):
def __init__(self,vehicles,cables):
super(Build,self).__init__()
self.input = vehicles
self.cables = cables

self.worker(cables)

def add_details(self,vehicle):
data = {
"ob_charger": {
"plug": vehicle.data["Charge_Plug"],
"power": str(vehicle.data["Charge_Standard_Power"]),
"phase": str(vehicle.data["Charge_Standard_Phase"]),
"charge_time": str(vehicle.data["Charge_Standard_ChargeTime"])
}
}

return data

def add_cables(self,vehicle):
data = vehicle.compatibility(self.cables)
return data

def worker(self,cables):
for i in self.input:
vehicle = VehicleData(self.input[i])
if(not vehicle.valid):
continue

output = self.add(vehicle.data["Vehicle_Make"],vehicle.data["Vehicle_Model"],vehicle.data["Availability_Date_From"][3:])

# Append vehicle data
output["details"] = self.add_details(vehicle)
output["cables"] = self.add_cables(vehicle)

15 changes: 9 additions & 6 deletions classes/Functions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import json
from pathlib import Path

# Import a JSON file
def load_JSON(f):
# Attempt to load JSON into memory
def load(self,f):
if not Path(f).is_file():
return False
raise IOError(f"Input file '{x}' not found")

with open(f) as f:
data = json.load(f)
return json.load(f)

return data
# Map key,value list
def forEach(list,func):
for i,v in enumerate(list):
func(v,i,list)
3 changes: 2 additions & 1 deletion classes/Import.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class File:

def fetch():
http = httplib2.Http()
response, content = http.request(File.endpoint, "GET")
response, content = http.request(File.endpoint,"GET")

handle = open(File.dataPath,"w")
handle.write(content.decode("utf-8"))
Expand Down Expand Up @@ -44,6 +44,7 @@ def validVehicle(vehicle):

return True

# Return compatible cables based on type, ampere and phase
def compatibleCables(type,ampere,phase):
# Type 1
if(type == "Type 1"):
Expand Down
28 changes: 19 additions & 9 deletions classes/Merge.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import json

from .Functions import *
from .settings import *

# Merge insert two objects
# Merge new vehicle data with existing vehicle data
class Merge:
def __init__(self,data_from,data_to):
self.input = self.assigned_list(data_from) # Merge this..
self.output = data_to # ..With this

# Merge lists
self.output.update(self.input)

def __init__(self,this,that):
self.data_target = load_JSON(data_vehicles)
self.data_insert = load_JSON(data_vehicles_new)
# Create assigned list from EVDB array of objects
def assigned_list(self,data):
output = {}
for vehicle in data:
key = str(vehicle["Vehicle_ID"])
output[key] = vehicle
return output

def run(self):
print(data_vehicles_new)
# Save merged output to file
def write(self,dest):
with open(dest,"w") as f:
json.dump(self.output,f)
return True
34 changes: 34 additions & 0 deletions classes/Vehicle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class VehicleData:
def __init__(self,data):
self.valid = True
self.data = data

if(data["Availability_Status"] > 1):
self.valid = False

# Return compatible cables ordered by best-fit
def compatibility(self,cables):
plug = self.data["Charge_Plug"]
phase = self.data["Charge_Standard_Phase"]
ampere = self.data["Charge_Standard_PhaseAmp"]

output = []

# -- Best-fit hierarchy --

# Plug; Type 1
if(plug == "Type 1"):
output = cables["Type 1"]["1-Phase"]["16A"]
return output

# Plug; Type 2
if(phase > 1):
if(ampere > 16):
output = cables["Type 2"]["3-Phase"]["32A"] + output
output = output + cables["Type 2"]["3-Phase"]["16A"]

if(ampere > 16):
output = output + cables["Type 2"]["1-Phase"]["32A"]
output = output + cables["Type 2"]["1-Phase"]["16A"]

return output
6 changes: 2 additions & 4 deletions classes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from .settings import *

from .Functions import *
from .Merge import *
from .Merge import Merge
from .Build import Build
8 changes: 0 additions & 8 deletions classes/settings.py

This file was deleted.

2 changes: 1 addition & 1 deletion public/vehicles.php → public/vehicles.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
];
}

echo json_encode($output);
echo $output;

?>
42 changes: 40 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
import os
import json
from shutil import copyfile
from pathlib import Path

import classes

merge = classes.Merge()
merge.run()
# Fetch raw data from this endpoint
endpoint = os.environ.get("ECHARGE_CONFIGURATOR_ENDPOINT")

files = {
"vehicles_processed": "data/vehicles.json",
"vehicles_stored": "data/vehicles_stored.json",
"vehicles_new": "data/vehicles_new.json",
"cables": "cables.json"
}

# Load JSON into memory
def load_json(f):
if not Path(f).is_file():
raise IOError(f"Input file '{x}' not found")

with open(f) as f:
return json.load(f)

def backup(f):
try:
copyfile(f,f"{f}.backup")
except IOError as error:
print(error)

# Clone existing vehicle archives before processing
backup(files["vehicles_processed"])
backup(files["vehicles_stored"])

# Merge new vehicle data with stored vehicle data
merge = classes.Merge(load_json(files["vehicles_new"]),load_json(files["vehicles_stored"]))
merge.write(files["vehicles_stored"]) # Overwrite archive with new data

# Create echarge-configurator compatible JSON from vehicle and cable data
process = classes.Build(merge.output,load_json(files["cables"]))
process.write(files["vehicles_processed"])

0 comments on commit 0f78abd

Please sign in to comment.