{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tb_device_mqtt import TBDeviceMqttClient, TBPublishInfo\n",
    "from datetime import datetime as dt\n",
    "import utilities\n",
    "from dataCollector import EthernetIP, Modbus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def serverRPC(client, request_id, request_body):\n",
    "    print(client)\n",
    "    print(request_id)\n",
    "    print(request_body)\n",
    "    #use request_body to determine action\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = utilities.load_json_file(\"./config.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [],
   "source": [
    "telemetry = {}\n",
    "client = TBDeviceMqttClient(config[\"mqttDomain\"])\n",
    "client._client.username_pw_set(config[\"mqttUserName\"],config[\"mqttPassword\"])\n",
    "client._client._client_id = config[\"mqttClientId\"]\n",
    "client.set_server_side_rpc_request_handler(serverRPC)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'vendor': 'Rockwell Automation/Allen-Bradley', 'product_type': 'Programmable Logic Controller', 'product_code': 180, 'revision': {'major': 12, 'minor': 11}, 'status': b'4\\x00', 'serial': '60ed86ac', 'product_name': '2080-LC20-20QWB', 'keyswitch': 'UNKNOWN'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'pond1Height': -32.0,\n",
       " 'pond2Height': -17.29999542236328,\n",
       " 'pond3Height': 0.0,\n",
       " 'pond4Height': 0.0,\n",
       " 'pond1Volume': 0.0,\n",
       " 'pond2Volume': 0.0,\n",
       " 'pond3Volume': 0.0,\n",
       " 'pond4Volume': 0.0,\n",
       " 'cfgNumberOfPonds': 3}"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver = EthernetIP(\"192.168.2.158\")\n",
    "with driver.driver:\n",
    "    print(driver.driver.get_plc_info())\n",
    "read = driver.read_tags(['pond1Height','pond2Height','pond3Height','pond4Height', 'pond1Volume','pond2Volume','pond3Volume','pond4Volume', \"cfgNumberOfPonds\"])\n",
    "for ind, k in enumerate(['pond1Height','pond2Height','pond3Height','pond4Height', 'pond1Volume','pond2Volume','pond3Volume','pond4Volume', \"cfgNumberOfPonds\"]):\n",
    "    telemetry[k] = read[ind][1]\n",
    "#driver.write_tags([\"cfgNumberOfPonds\",\"pond2Height\"], [3,2])\n",
    "telemetry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client.connect()\n",
    "while True:\n",
    "    if dt.timestamp(dt.now()) % config[\"publishInterval\"] == 0:\n",
    "        print(dt.now())\n",
    "        client.send_telemetry(telemetry)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1659040728.331675\n",
      "1659040728\n",
      "1659040728\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'truncate' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m/Users/nico/Documents/GitHub/ThingsBoard/ThingsBoardClient/tbclient.ipynb Cell 7\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/nico/Documents/GitHub/ThingsBoard/ThingsBoardClient/tbclient.ipynb#ch0000011?line=3'>4</a>\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mint\u001b[39m(now))\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/nico/Documents/GitHub/ThingsBoard/ThingsBoardClient/tbclient.ipynb#ch0000011?line=4'>5</a>\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mround\u001b[39m(now))\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/nico/Documents/GitHub/ThingsBoard/ThingsBoardClient/tbclient.ipynb#ch0000011?line=5'>6</a>\u001b[0m \u001b[39mprint\u001b[39m(truncate(now))\n",
      "\u001b[0;31mNameError\u001b[0m: name 'truncate' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "while True:\n",
    "    now = dt.timestamp(dt.now())\n",
    "    print(now)\n",
    "    print(int(now))\n",
    "    print(round(now))\n",
    "    #print(truncate(now))\n",
    "\n",
    "#% config[\"publishInterval\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client.send_telemetry(telemetry)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client.disconnect()"
   ]
  }
 ],
 "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
}
