{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tb_rest_client.rest_client_pe import *\n",
    "from tb_rest_client.rest import ApiException\n",
    "import base64\n",
    "from datetime import datetime as dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertDateTimeToMS(datestring):\n",
    "        date = dt.strptime(datestring,\"%d %b %Y, %H:%M:%S\")\n",
    "        return int(date.timestamp() * 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://hp.henrypump.cloud\"\n",
    "username = \"nmelone@henry-pump.com\"\n",
    "password = \"gzU6$26v42mU%3jDzTJf\"\n",
    "with RestClientPE(base_url=url) as rest_client:\n",
    "    try:\n",
    "        rest_client.login(username=username, password=password)\n",
    "        customer = rest_client.get_customers(page_size=10,page=0,text_search=\"Amerus Safety Solutions\")\n",
    "        #print(customer)\n",
    "        cid = customer.data[0].id.id\n",
    "        device = rest_client.get_customer_devices(customer_id=cid, page=0, page_size=10,text_search=\"Camera Trailer 107\")\n",
    "        #print(device)\n",
    "        eType = device.data[0].id.entity_type\n",
    "        eid = device.data[0].id.id\n",
    "        start = convertDateTimeToMS(\"28 Jul 2022, 00:00:00\")\n",
    "        end = int(dt.now().timestamp() * 1000)\n",
    "        telemetry = rest_client.get_timeseries(entity_type=eType, entity_id=eid , keys=\"snapshot\", start_ts=start, end_ts=end, limit=1000)\n",
    "        #print(telemetry)\n",
    "    except ApiException as e:\n",
    "        print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "for snap in telemetry[\"snapshot\"]:\n",
    "    with open('/Users/nico/Documents/tbTimelapse/'+ str(snap[\"ts\"]) + \".jpg\", 'wb') as img:\n",
    "        img.write(base64.b64decode(snap[\"value\"]))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "32b1684233d9748bd1bb5a29a1b19459c9564d6488d1324e633b9c48826c5d03"
  },
  "kernelspec": {
   "display_name": "Python 3.10.4 ('thingsboard')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
