Skip to content

Commit

Permalink
POC to POE
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnathanson committed Jan 25, 2021
1 parent d670d75 commit cd16ec6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 47 deletions.
1 change: 0 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ Python Example:
```
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
Expand Down
24 changes: 12 additions & 12 deletions backend/api/v1/clientPostIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

localConfig = "/home/pi/local/local.json";

pocLog = "/home/pi/solar-protocol/backend/api/v1/poc.log"
pocData = []
poeLog = "/home/pi/solar-protocol/backend/api/v1/poe.log"
poeData = []

myIP = requests.get('http://whatismyip.akamai.com/').text

Expand Down Expand Up @@ -48,30 +48,30 @@ def getIPList():

return ipList

def getPocLog():
def getPoeLog():

try:
pocFile = open(pocLog)
poeFile = open(poeLog)

pocFileLines = pocFile.readlines()
poeFileLines = poeFile.readlines()

#read the most recent 50 lines
for l in range(100):

#print(pocFileLines[l])
#print(poeFileLines[l])

#remove "INFO:root:" from the string and strip spaces
pocData.append(pocFileLines[len(pocFileLines)-l-1][10:-1])
poeData.append(poeFileLines[len(poeFileLines)-l-1][10:-1])

#if l > 100:
# break

pocFile.close()
poeFile.close()

except:
pocData.append(0)
poeData.append(0)

#print(pocData)
#print(poeData)

def getLocalConfig(key):

Expand All @@ -90,7 +90,7 @@ def getLocalConfig(key):

def makePosts(ipList):

myString = "api_key="+apiKey+"&stamp="+str(time.time())+"&ip="+myIP+"&mac="+myMAC+"&name="+myName+"&log="+','.join(pocData)
myString = "api_key="+apiKey+"&stamp="+str(time.time())+"&ip="+myIP+"&mac="+myMAC+"&name="+myName+"&log="+','.join(poeData)

print(myString)

Expand Down Expand Up @@ -118,7 +118,7 @@ def makePosts(ipList):
apiKey = getLocalConfig("apiKey")
#apiKey = os.getenv('SP_API_KEY')

getPocLog();
getPoeLog();
dstList = getIPList()
makePosts(dstList)

10 changes: 5 additions & 5 deletions backend/api/v1/solarProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@

localDataFile = "/home/pi/solar-protocol/charge-controller/data/tracerData"+ str(datetime.date.today()) +".csv"

logging.basicConfig(filename='/home/pi/solar-protocol/backend/api/v1/poc.log', level=logging.INFO)
logging.basicConfig(filename='/home/pi/solar-protocol/backend/api/v1/poe.log', level=logging.INFO)

#return data from a particular server
def getData(dst):
try:
#returns a single value
response = requests.get('http://' + dst + '/api/v1/api.php?value='+apiValue, timeout = 5)
response = requests.get('http://' + dst + '/api/v1/chargecontroller.php?value='+apiValue, timeout = 5)
#print(response.text)
#check if the response can be converted to a float
# try:
# response = requests.get('http://' + dst + '/api/v1/api.php?value='+apiValue, timeout = 5)
# response = requests.get('http://' + dst + '/api/v1/chargecontroller.php?value='+apiValue, timeout = 5)
#this was in the try
return float(response.text)
# except:
Expand Down Expand Up @@ -77,14 +77,14 @@ def determineServer():
thisServer = False

if thisServer:
print('Point of contact')
print('Point of entry')

logging.info(datetime.datetime.now())

#comment back in to run
os.system(subCall)
else:
print('Not point of contact')
print('Not point of entry')
#logging.info(datetime.datetime.now())#comment this out after testing

def localData():
Expand Down
50 changes: 25 additions & 25 deletions frontend/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<div id="server list"><h2>Servers:</h2></div>

<div id="pointOfContact"><h2>Point of Contact History:</h2></div>
<!-- <div id="poc_chart" style="width: 1500px; height: 500px"></div> -->
<!-- <div id="poe_chart" style="width: 1500px; height: 500px"></div> -->

<script>
//make this dynamic at some point
Expand All @@ -30,7 +30,7 @@
//get the most recent line of charge controller data
let toGet = "0";

let jsonPoc;
let jsonPoe;


let devListURL = "http://"+ window.location.hostname +"/api/v1/api.php?file=deviceList";
Expand All @@ -39,7 +39,7 @@
getRequest(devListURL,parseDevList);

//point of contact
//getRequest(pocURL,sortPocLog);
//getRequest(poeURL,sortPoeLog);


function getRequest(dst, callback){
Expand Down Expand Up @@ -68,38 +68,38 @@ function parseDevList(response){
//individual server data
for (let i = 0; i < ipList.length; i++){
//pingServer(tempIPList[i], populate);
let requestURL = "http://" + ipList[i] + "/api/v1/api.php?line="+toGet;
let requestURL = "http://" + ipList[i] + "/api/v1/chargecontroller.php?line="+toGet;
getRequest(requestURL, populate);
}

//point of contact
sortPocLog(response);
sortPoeLog(response);
}

function sortPocLog(response){
function sortPoeLog(response){
console.log(JSON.parse(response));
jsonPoc = JSON.parse(response);
jsonPoe = JSON.parse(response);

let justPocLog = []
let justPoeLog = []

let storePos = [];//store the position for that particular log

//get just the logs
for (let p = 0; p < jsonPoc.length;p++){
justPocLog.push(jsonPoc[p]["log"]);
for (let p = 0; p < jsonPoe.length;p++){
justPoeLog.push(jsonPoe[p]["log"]);
storePos[p] = 0;
}

let outputPocLog = [];
let outputPoeLog = [];

for (let p = 0; p < 100;p++){

//get next items from logs
let tempPos = [];
let sorted = [];
for (let l = 0; l < storePos.length;l++){
tempPos[l] = justPocLog[l][storePos[l]];
sorted[l] = justPocLog[l][storePos[l]];
tempPos[l] = justPoeLog[l][storePos[l]];
sorted[l] = justPoeLog[l][storePos[l]];
}

//sort in decending order
Expand All @@ -112,41 +112,41 @@ function sortPocLog(response){
for (let x = 0; x < tempPos.length; x++){
if (sorted[0] == tempPos[x]){
//add new value to list only if it has changed...
if(outputPocLog.length == 0){
outputPocLog[outputPocLog.length] = [justPocLog[x][storePos[x]],x];
} else if(outputPocLog[outputPocLog.length-1][1] != x){
outputPocLog[outputPocLog.length] = [justPocLog[x][storePos[x]],x];
if(outputPoeLog.length == 0){
outputPoeLog[outputPoeLog.length] = [justPoeLog[x][storePos[x]],x];
} else if(outputPoeLog[outputPoeLog.length-1][1] != x){
outputPoeLog[outputPoeLog.length] = [justPoeLog[x][storePos[x]],x];
}
storePos[x]++;
break;
}
}
}

//console.log(outputPocLog);
//console.log(outputPoeLog);

displayPOC(outputPocLog);
displayPOE(outputPoeLog);

}

function displayPOC(pocArray){
let pocID = document.getElementById('pointOfContact');
function displayPOE(poeArray){
let poeID = document.getElementById('pointOfContact');

let para = document.createElement('p');

for (let l = 0; l < pocArray.length;l++){
let node = document.createTextNode(pocArray[l][0] + " " + jsonPoc[pocArray[l][1]]['name']);
for (let l = 0; l < poeArray.length;l++){
let node = document.createTextNode(poeArray[l][0] + " " + jsonPoe[poeArray[l][1]]['name']);
para.appendChild(node);
para.appendChild(document.createElement('br'));//dont use a variable here, because then it will treat it as the same thing and only append it once, pushing it to the end of the p
}

pocID.appendChild(para);
poeID.appendChild(para);
}


function populate(dataToDisplay, dst) {

let dstIP = dst.replace('/api/v1/api.php?line=0','');
let dstIP = dst.replace('/api/v1/chargecontroller.php?line=0','');
//dstIP = dst.replace('http://','');

const sList = document.getElementById('server list');
Expand Down
8 changes: 4 additions & 4 deletions frontend/api/v1/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
fclose($fp);
}
else {
echo "Wrong API Key provided.";
echo "Wrong API Key provided.";
}
} else if ($_SERVER["REQUEST_METHOD"] == "GET") {

Expand All @@ -111,7 +111,7 @@
}
}
//get a line of current data file. "len" returns length of current file, "head" returns the column headers, "0" returns most recent line. Increments up for other lines.
else if (array_key_exists("line", $_GET)) {
/*else if (array_key_exists("line", $_GET)) {
//echo "Key = Line";
$readData = chargeControllerData();
Expand All @@ -137,7 +137,7 @@
}
//get a full file
} else if (array_key_exists("file", $_GET)) {
}*/ /*else if (array_key_exists("file", $_GET)) {
//echo "Key = File";
if($_GET["file"] == "deviceList"){ //deviceList should be a POST
Expand Down Expand Up @@ -167,7 +167,7 @@
} else if(strpos($_GET["file"],'tracerData') !== false){ //get CC data file by file name
var_dump(str_getcsv(getFile($ccDir . $_GET["file"] . '.csv')));
}
}
}*/
}

function justTracerDataFiles($dir){
Expand Down

0 comments on commit cd16ec6

Please sign in to comment.