Duplicate the chart export functionality with Web Read API
Trying to duplicate the chart 'Export' data functionality with the read api. I am using python with the requests library. I have tried both raw data and processed data, explicitly setting the useTimeExtension to True even though that is the default. I get varying amounts of data from the read. Sometimes it will give me data for all the timestamps, sometimes it will give me data for some of the timestamps and 'None' for the rest, and sometimes it will return all values as 'None'. For data that doesn't change frequently, if I use raw data then I get an empty list of values, if I use the processed data I get 'None' values for all the timestamps. The continuation field is empty on the return, so I think it is reading all the data. Exporting on a chart with the same timeframes gives me the data I want. What is the proper way to duplicate that functionality?
# These variables should be put in a 'secrets' file to hide them
GOOD = 192 # Define the tvq quality value for good
READ_PORT = 55236
READ_VERSION = "/api/v2"
class Canary:
def __init__(self, historian=DEFAULT_HISTORIAN):
self.historian = historian
self.tag = None
self.value = None
self.user_token_str = None
self.session_token_str = None
def read_tag(self, tagList):
self.tagList = tagList
data = self._read_from_canary()
return data
def _read_from_canary(self):
store_data_url = self._build_url("/getTagData")
store_data_payload = {
"apiToken": API_TOKEN,
"tags": self.tagList,
"startTime": "Now - 10Seconds",
"endTime": "Now",
"aggregateName": "TimeAverage2",
"aggregateInterval": "00:00:01",
"useTimeExtension": "true"
}
store_data_json = self._get_message(store_data_url, store_data_payload)
return store_data_json['data']
def _build_url(self, endpoint):
return f"https://{self.historian}:{READ_PORT}{READ_VERSION}{endpoint}"
@staticmethod
def _post_message(url, payload):
try:
response = requests.post(url, json=payload, verify=False)
except requests.exceptions.Timeout:
# Maybe set up for a retry, or continue in a retry loop
raise CanaryReadError("Request Timed out")
except requests.exceptions.TooManyRedirects:
raise CanaryReadError("Bad URL")
except requests.exceptions.RequestException as e:
raise SystemExit(e)
else:
json_text = json.loads(response.text)
if json_text['statusCode'] != 'Good':
raise CanaryReadError("Bad Status Code: " + json_text['statusCode'])
else:
return json_text
@staticmethod
def _get_message(url, payload):
try:
response = requests.get(url, params=payload, verify=False)
except requests.exceptions.Timeout:
# Maybe set up for a retry, or continue in a retry loop
raise CanaryReadError("Request Timed out")
except requests.exceptions.TooManyRedirects:
raise CanaryReadError("Bad URL")
except requests.exceptions.RequestException as e:
raise SystemExit(e)
else:
json_text = json.loads(response.text)
if json_text['statusCode'] != 'Good':
raise CanaryReadError("Bad Status Code: " + json_text['statusCode'])
else:
return json_text
1 reply
-
I expanded the selection to Now-120Seconds and here is the last bit of the response:
{'t': '2025-05-16T08:23:36.0403205-04:00', 'v': 56.83846311137004}, {'t': '2025-05-16T08:23:37.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:38.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:39.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:40.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:41.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:42.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:43.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:44.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:45.0403205-04:00', 'v': None}, {'t': '2025-05-16T08:23:46.0403205-04:00', 'v': None}]
The first line shows data and all previous timestamps had the same value, but the last 10 timestamps return 'None' as the value