Snippets
Created by
David Sánchez
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | import pytz
import requests
import datetime
import time
# Settings
TOKEN = "BBFF-W8MwKjIR5yOUoI1IJVFzUOQBaWsIwZ"
""" DEVICE = "prices"
DEVICE_MEAN_PRICES = "mean-prices" """
DEVICE = "prices-test"
DEVICE_MEAN_PRICES = "mean-prices-test"
VARIABLES = ["tas1", "qld1", "vic1", "sa1", "nsw1"]
TIMEZONE = "Australia/Canberra"
def get_ubi_data(token, device, variable,
lv=False, page_size=1, start=None, end=None):
url = "http://industrial.api.ubidots.com"
url = "{}/api/v1.6/devices/{}/{}".format(url, device, variable)
if lv:
url = "{}/lv".format(url)
else:
url = "{}/values?page_size={}".format(url, page_size)
if start is not None:
url = "{}&start={}".format(url, start)
if end is not None:
url = "{}&end={}".format(url, end)
headers = {"X-Auth-Token": token, "Content-Type": "application/json"}
status = 400
attempts = 0
while status >= 400 and attempts <= 5:
req = requests.get(url=url, headers=headers)
status = req.status_code
attempts += 1
if status >= 400:
time.sleep(1)
if status == 200 or status == 201:
print("[INFO] Data obtained properly")
return None if len(req.content) == 0 else req.json()
print("[ERROR] Could not retrieve data")
return None
def get_minute(timestamp):
date_obj = datetime.datetime.fromtimestamp(timestamp)
return (date_obj.minute)
def mean(values):
return float(sum(values)) / max(len(values), 1)
def parse_date(orig_date, time_zone):
'''
Parses the date and returns a timestamp in milliseconds localized in
the specified timezone
date format must be 2018-03-17T02:05:00
'''
date, hour = orig_date.split("T")
date_formated = "{} {}".format(date, hour)
date_obj = datetime.datetime.strptime(date_formated, "%Y-%m-%d %H:%M:%S")
tz = pytz.timezone(time_zone)
timestamp = round(tz.localize(date_obj).timestamp()*1000)
return timestamp
def calculateAverage(timestamp, data):
minutes = get_minute(int(timestamp / 1000))
# If it is within the first 5 minutes
if minutes in range(0, 4) or minutes in range(30, 34):
return 0, False
# If there is no valid data
if (len(data) == 0):
return 0, False
# The average of the valid data is calculated
return float(sum(data)) / max(len(data), 1), True
def parse_price(last_value, data):
timestamp_aemo_api = last_value['timestamp']
timestamp_aest = parse_date(last_value['context']['change_timestamp'], TIMEZONE)
timestampDiference = ((timestamp_aemo_api - timestamp_aest) / 1000)
test_1 = last_value['value'] >= -1000 and last_value['value'] <= 16000
test_2 = len(str(last_value['value'])) == 8 and timestampDiference >= 150 and timestampDiference <= 300
# The price is calculated normal
if test_1 and test_2:
return -1000000, True
values = [dot.get('value') for dot in data if dot.get('context').get('state') is None]
# It is calculated with the timestamp of ubidots
if not test_1 and test_2:
return calculateAverage(timestamp_aemo_api, values)
# It is calculated with the AEST timestamp
return calculateAverage(timestamp_aest, values)
def localize_date(date_obj, time_zone):
tz = pytz.timezone(time_zone)
date_obj_localized = pytz.utc.localize(date_obj).astimezone(tz)
return date_obj_localized
def get_formatted_context(date_obj, time_zone):
date_obj_localized = localize_date(date_obj, time_zone)
date = "{}-{}-{}T{}:{}:{}".format(
date_obj_localized.year,
date_obj_localized.month,
date_obj_localized.day,
date_obj_localized.hour,
date_obj_localized.minute,
date_obj_localized.second
)
return date
def send_to_ubidots(token, device, payload):
url = "http://industrial.api.ubidots.com"
url = "{}/api/v1.6/devices/{}".format(url, device)
headers = {"X-Auth-Token": token, "Content-Type": "application/json"}
status = 400
attempts = 0
print("[INFO] attempting to send data to Ubidots")
while status >= 400 and attempts <= 5:
req = requests.post(url=url, headers=headers, json=payload)
status = req.status_code
attempts += 1
if status >= 400:
time.sleep(1)
if status == 200 or status == 201:
print("[INFO] Data posted properly")
return (True, req.json())
print("[ERROR] Could not post data")
return (False, ())
def get_page_size(minute):
# Number of dots to retrieve
if minute < 5:
page_size = 6
if minute >= 5 and minute < 35:
page_size = int(minute/5)
if minute >= 35 and minute < 60:
page_size = int((minute - 30) / 5) # Number of dots to retrieve
# Obtains up to six values since the init date
if page_size == 0:
page_size = 1
return page_size
def create_payload(data, init_date_obj, page_size):
values = []
results = []
tz = pytz.timezone(TIMEZONE)
date_obj_localized = pytz.utc.localize(init_date_obj).astimezone(tz)
init_timestamp = int(date_obj_localized.timestamp())
# boundarie timestamp should be up to 30 minutes ago
up_boundary_timestamp = init_timestamp + (5 * 60)
low_boundary_timestamp = init_timestamp - (60 * 5 * (page_size))
for dot in data:
timestamp = int(dot["timestamp"] / 1000)
if timestamp <= up_boundary_timestamp \
and timestamp >= low_boundary_timestamp:
value = dot["value"]
values.append(value)
results.append(dot)
return values, results
def check_period_change(token):
message = "[INFO] Attempting to obtain variable=last-period-scrapped"
message = "{} from device1=mean-prices, device2=prices".format(message)
print(message)
last_price_period = get_ubi_data(token,
DEVICE,
"last-period-scrapped",
lv=True)
last_mean_price_period = get_ubi_data(token,
DEVICE_MEAN_PRICES,
"last-period-scrapped",
lv=True)
if last_price_period is None or last_mean_price_period is None:
print("[ERROR] Culd not obtain last scrapped periods")
return False
message = "[INFO] Result obtained for device1={}".format(last_price_period)
message = "{} , device2={}".format(message, last_mean_price_period)
print(message)
return last_mean_price_period != last_price_period
def parse_ubi_data(token, device, variables):
mean_prices = {}
init_date_obj = datetime.datetime.utcnow()
for variable in variables:
# Obtains last stored timestamp
print("[INFO] Attempting to obtain data from {}".format(variable))
result = get_ubi_data(token, device, variable)["results"][0]
if result is None:
print("[ERROR] Could not retrieve data")
return None
timestamp = int(result["timestamp"] / 1000)
print("[INFO] timestamp from variable: {}".format(variable))
minute = get_minute(timestamp)
page_size = get_page_size(minute)
print("[INFO] Attempting to retrieve {} values from Ubidots".format(
page_size)
)
data = get_ubi_data(token, device, variable,
end=result['timestamp'], page_size=page_size)
if data is None:
print("[ERROR] Could not retrieve data")
return None
data = data["results"]
print("[INFO] Ubidots data retrieved:\n{}".format(data))
values, results = create_payload(data, init_date_obj, page_size)
if len(values) <= 0:
print("[ERROR] Data retrieved is not consistent with the period to calculate")
return None
timestamp = data[0]['timestamp'] # Last registered timestamp
print("[INFO] timestamp for payload: {}".format(timestamp))
date_formatted = get_formatted_context(init_date_obj, TIMEZONE)
# Check tests
value, is_valid = parse_price(result, results)
average = value
if is_valid and value == -1000000:
average = mean(values)
context = {"update-date": date_formatted}
if not is_valid:
context["state"] = "No value"
mean_prices[variable] = {"value": average,
"timestamp": timestamp,
"context": context
}
minute = get_minute(int(timestamp/1000))
actual_period = int(minute/5)
mean_prices['last-period-scrapped'] = {"value": actual_period,
"timestamp": timestamp,
"context": {"update-date": date_formatted}
}
return mean_prices
def main(args):
mean_prices = None
print("[INFO] Checking if period has changed")
if check_period_change(TOKEN):
print("[INFO] Period changed, triggering average rolling calculus")
mean_prices = parse_ubi_data(TOKEN, DEVICE, VARIABLES)
print("[INFO] payload to send: {}".format(mean_prices))
if mean_prices is not None:
print("[INFO] Attempting to send data to Ubidots")
return {"status": "ok",
"result": send_to_ubidots(TOKEN, DEVICE_MEAN_PRICES, mean_prices)}
else:
print("[INFO] Period has not changed or could not retrieve info from Ubidots")
return {"status": "ok", "result": None}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.