265 lines
13 KiB
Python
265 lines
13 KiB
Python
#Mistaway data collection
|
|
import lattice, logging
|
|
from shapely.geometry import Point, Polygon
|
|
from datetime import datetime as dt
|
|
|
|
logger = logging.getLogger('billing_reports')
|
|
logger.setLevel(logging.INFO)
|
|
ch = logging.StreamHandler()
|
|
ch.setLevel(logging.INFO)
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
ch.setFormatter(formatter)
|
|
logger.addHandler(ch)
|
|
|
|
# Find a value for a given key in a given dictionary
|
|
def _findItem(obj, key):
|
|
if key in obj: return obj[key]
|
|
for k, v in obj.items():
|
|
if isinstance(v,dict):
|
|
item = _findItem(v, key)
|
|
if item is not None:
|
|
return item
|
|
|
|
|
|
# recursively go through folders to build folder structure
|
|
def putFolder(folder, fs):
|
|
try:
|
|
if not folder["id"] == folder["parentFolderId"]:
|
|
parent = _findItem(fs, folder["parentFolderId"])
|
|
parent[folder["id"]] = folder
|
|
putFolder(parent,fs)
|
|
else:
|
|
fs[folder["id"]] = folder
|
|
return fs
|
|
except Exception as e:
|
|
logger.debug(f"Exception in putFolder: {e}")
|
|
|
|
def getMistAwayData():
|
|
# Go through every folder and build a proper folder structure
|
|
# Output to JSON file
|
|
nodes = lattice.getNodes()
|
|
folders = lattice.getFolders()
|
|
foldermap = {}
|
|
for folder in folders:
|
|
logger.debug(folder)
|
|
putFolder(folder,foldermap)
|
|
|
|
# Go through every node and collect unique device id (MAC)
|
|
# Output to JSON file
|
|
foldersTracker = []
|
|
vanityMap = {}
|
|
deviceTypes = {}
|
|
for type in lattice.getNodeTypes():
|
|
deviceTypes[type["id"]] = type["name"]
|
|
for node in nodes:
|
|
if not node["uniqueId"][-6:] in [":00:00", ":00:30"]:
|
|
if not node["folderId"] in foldersTracker:
|
|
foldersTracker.append(node["folderId"])
|
|
folder = _findItem(foldermap, node["folderId"])
|
|
if folder:
|
|
deviceName = folder["name"]
|
|
latitude = folder["location"]["lat"]
|
|
longitude = folder["location"]["lng"]
|
|
pfolder = _findItem(foldermap, folder["parentFolderId"])
|
|
ppfolder = _findItem(foldermap, pfolder["parentFolderId"])
|
|
customer = ppfolder["name"]
|
|
vanityMap[node["uniqueId"]] = {"deviceName": deviceName, "deviceType": deviceTypes[node["nodeTypeId"]], "customer": customer, "latitude": latitude, "longitude": longitude}
|
|
else:
|
|
logger.info("Folder does not exist: " + str(node["folderId"]))
|
|
else:
|
|
logger.info("Folder already in list: " + str(node["folderId"]))
|
|
|
|
"""
|
|
Data to be collected:
|
|
{
|
|
"customer":{
|
|
"sales_order": {
|
|
"billable_item_1": {
|
|
"sales_price": 75,
|
|
"platform_cost": 10,
|
|
"platform": "thingsboard", # "thingsboard", "mistaway"
|
|
"cellular_cost": 15,
|
|
"billing_type": "stand-alone" # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
},
|
|
"billable_item_2:{...},
|
|
...
|
|
},
|
|
"sales_order_2":{...}
|
|
},
|
|
"customer_2":{...}
|
|
}
|
|
"""
|
|
|
|
mistaway_data = {}
|
|
|
|
denali_east = Polygon([(31.441289, -102.175343),(31.467676, -101.936571), (31.321496, -101.943604),(31.304714, -102.139878) ])
|
|
for _, value in vanityMap.items():
|
|
customer = value["customer"]
|
|
device = value["deviceName"]
|
|
type = value["deviceType"]
|
|
location = Point(value["latitude"], value["longitude"])
|
|
if not device in ["Melinda 252"]:
|
|
cellular_cost = 20
|
|
so = "HPSO-1"
|
|
price = 75
|
|
billing_type = "Stand-Alone"
|
|
if customer not in mistaway_data:
|
|
mistaway_data[customer] = {}
|
|
|
|
if customer == "CrownQuest":
|
|
if device == "LimeQuest 6 SR 1-1":
|
|
so = "LimeQuest SO"
|
|
price = 75
|
|
billing_type = "Stand-Alone"
|
|
elif "Wilkinson 39" in device or device in ["Wilkinson 37 WS 1-9B", "Wilkinson 37 WS 1-9B", "Free 40 WS 1-2", "Free 40 WS 1-4", "Free 40 WS 1-1"]:
|
|
so = "Wilkinson 39 Field"
|
|
price = 0
|
|
billing_type = "AP-bundled"
|
|
cellular_cost = 0
|
|
if so not in mistaway_data[customer]:
|
|
mistaway_data[customer][so] = {}
|
|
if "Wilkinson 39 AP" not in mistaway_data[customer][so]:
|
|
mistaway_data[customer][so]["Wilkinson 39 AP"] = {
|
|
"Sales Price": 250,
|
|
"Platform Cost": 0,
|
|
"Platform": "Mistaway", # "thingsboard", "mistaway"
|
|
"Cellular Cost": 20,
|
|
"Billing Type": "AP" # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
}
|
|
elif "Wilkinson 37" in device or device in ["Wilkinson 33 WS 3-1", "Wilkinson 33 WS 4-1", "Wilkinson 34 WS 2-10", "Wilkinson 34 WS 1-8"]:
|
|
so = "Wilkinson 37 Field"
|
|
price = 0
|
|
billing_type = "AP-bundled"
|
|
cellular_cost = 0
|
|
if so not in mistaway_data[customer]:
|
|
mistaway_data[customer][so] = {}
|
|
if "Wilkinson 37 AP" not in mistaway_data[customer][so]:
|
|
mistaway_data[customer][so]["Wilkinson 37 AP"] = {
|
|
"Sales Price": 250,
|
|
"Platform Cost": 0,
|
|
"Platform": "Mistaway", # "thingsboard", "mistaway"
|
|
"Cellular Cost": 20,
|
|
"Billing Type": "AP" # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
}
|
|
elif "Wilkinson 33" in device or "Wilkinson 34" in device or "Wilkinson 1" in device or "Wilkinson 4" in device:
|
|
so = "Wilkinson 33-34 Field"
|
|
price = 0
|
|
billing_type = "AP-bundled"
|
|
cellular_cost = 0
|
|
if so not in mistaway_data[customer]:
|
|
mistaway_data[customer][so] = {}
|
|
if "Wilkinson 33-34 AP" not in mistaway_data[customer][so]:
|
|
mistaway_data[customer][so]["Wilkinson 33-34 AP"] = {
|
|
"Sales Price": 250,
|
|
"Platform Cost": 0,
|
|
"Platform": "Mistaway", # "thingsboard", "mistaway"
|
|
"Cellular Cost": 20,
|
|
"Billing Type": "AP" # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
}
|
|
elif "Free 40" in device or "Free 32" in device:
|
|
so = "Free Field"
|
|
price = 0
|
|
billing_type = "AP-bundled"
|
|
cellular_cost = 0
|
|
if so not in mistaway_data[customer]:
|
|
mistaway_data[customer][so] = {}
|
|
if "Free AP" not in mistaway_data[customer][so]:
|
|
mistaway_data[customer][so]["Free AP"] = {
|
|
"Sales Price": 250,
|
|
"Platform Cost": 0,
|
|
"Platform": "Mistaway", # "thingsboard", "mistaway"
|
|
"Cellular Cost": 20,
|
|
"Billing Type": "AP" # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
}
|
|
elif "LimeQuest 5" in device or "LimeQuest 10" in device:
|
|
so = "LimeQuest Field"
|
|
price = 0
|
|
billing_type = "AP-bundled"
|
|
cellular_cost = 0
|
|
if so not in mistaway_data[customer]:
|
|
mistaway_data[customer][so] = {}
|
|
if "LimeQuest AP" not in mistaway_data[customer][so]:
|
|
mistaway_data[customer][so]["LimeQuest AP"] = {
|
|
"Sales Price": 250,
|
|
"Platform Cost": 0,
|
|
"Platform": "Mistaway", # "thingsboard", "mistaway"
|
|
"Cellular Cost": 20,
|
|
"Billing Type": "AP" # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
}
|
|
elif "LimeQuest 5" in device or "LimeQuest 10" in device:
|
|
so = "LimeQuest Field"
|
|
price = 0
|
|
billing_type = "AP-bundled"
|
|
cellular_cost = 0
|
|
if so not in mistaway_data[customer]:
|
|
mistaway_data[customer][so] = {}
|
|
if "LimeQuest AP" not in mistaway_data[customer][so]:
|
|
mistaway_data[customer][so]["LimeQuest AP"] = {
|
|
"Sales Price": 250,
|
|
"Platform Cost": 0,
|
|
"Platform": "Mistaway", # "thingsboard", "mistaway"
|
|
"Cellular Cost": 20,
|
|
"Billing Type": "AP" # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
}
|
|
elif "Horton 23" in device or "Horton 34" in device:
|
|
so = "Horton Field"
|
|
price = 0
|
|
billing_type = "AP-bundled"
|
|
cellular_cost = 0
|
|
if so not in mistaway_data[customer]:
|
|
mistaway_data[customer][so] = {}
|
|
if "Horton AP" not in mistaway_data[customer][so]:
|
|
mistaway_data[customer][so]["Horton AP"] = {
|
|
"Sales Price": 250,
|
|
"Platform Cost": 0,
|
|
"Platform": "Mistaway", # "thingsboard", "mistaway"
|
|
"Cellular Cost": 20,
|
|
"Billing Type": "AP" # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
}
|
|
elif type == "advvfdipp" or type == "ipp":
|
|
so = "Santa Rosa"
|
|
elif type == "rigpump":
|
|
so = "Rig Pump"
|
|
elif type == "transferlite":
|
|
so = "Transfer"
|
|
elif customer == "Patriot Resources":
|
|
so = "Patriot Resources"
|
|
price = 100
|
|
elif customer == "Keagan Faudree Water":
|
|
so = "Keagan Faudree Water"
|
|
elif customer == "Summit Petroleum":
|
|
if location.x >= 31.654963:
|
|
so = "Banay"
|
|
price = 75
|
|
elif location.within(denali_east):
|
|
so = "Denali East"
|
|
if type == "dual_flowmeter":
|
|
price = 75
|
|
elif type == "plcfreshwater":
|
|
price = 55
|
|
cellular_cost = 0
|
|
billing_type = "Networked"
|
|
else: #if location.within(jitterbug):
|
|
so = "Jitterbug"
|
|
if type == "dual_flowmeter":
|
|
price = 75
|
|
elif type == "plcfreshwater":
|
|
price = 55
|
|
cellular_cost = 0
|
|
billing_type = "Networked"
|
|
|
|
|
|
if so not in mistaway_data[customer]:
|
|
mistaway_data[customer][so] = {}
|
|
|
|
mistaway_data[customer][so][device] = {
|
|
"Sales Price": price,
|
|
"Platform Cost": 10,
|
|
"Platform": "Mistaway", # "thingsboard", "mistaway"
|
|
"Cellular Cost": cellular_cost,
|
|
"Billing Type": billing_type # "stand-alone", "AP", "AP-bundled", "networked", "stand-alone-wifi"
|
|
}
|
|
return mistaway_data
|
|
|
|
|
|
|