{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#migration script to rule them all\n",
    "import boto3, json, lattice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertToNewConfig(config):\n",
    "    #old form\n",
    "    #\"{'mainHP': 'https://hp-drivers.s3-us-west-2.amazonaws.com/mainMeshify/','m1': 'https://hp-drivers.s3-us-west-2.amazonaws.com/M1/','flowmonitor': 'https://s3.amazonaws.com/pocloud-drivers/flow-monitor/'}\"\n",
    "\n",
    "    #new form\n",
    "    #\"{'mainHP': 'https://hp-thingsboard.s3.amazonaws.com/mainHP/','m1': 'https://hp-thingsboard.s3.amazonaws.com/M1/','flowmonitor': 'https://hp-thingsboard.s3.amazonaws.com/flowmonitor/'}\"\n",
    "    mainHPMapping = {\n",
    "        \"mainMeshify\": \"https://hp-thingsboard.s3.amazonaws.com/mainHP/\",\n",
    "        \"piflow\": \"https://hp-thingsboard.s3.amazonaws.com/mainHPRPI/\",\n",
    "        \"plcfresh\": \"https://hp-thingsboard.s3.amazonaws.com/mainHPPLCFRESH/\"\n",
    "    }\n",
    "\n",
    "    configMapping = {\n",
    "        \"abbflow\": \"https://hp-thingsboard.s3.amazonaws.com/abbflow/\",\n",
    "        \"advvfdipp\": \"https://hp-thingsboard.s3.amazonaws.com/advvfdipp/\",\n",
    "        \"dhsensor\": \"https://hp-thingsboard.s3.amazonaws.com/dhsensor/\",\n",
    "        \"dual_flowmeter\": \"https://hp-thingsboard.s3.amazonaws.com/dual_flowmeter/\",\n",
    "        \"flowmeterskid\": \"https://hp-thingsboard.s3.amazonaws.com/flowmeterskid/\",\n",
    "        \"flowmonitor\": \"https://hp-thingsboard.s3.amazonaws.com/flowmonitor/\",\n",
    "        \"PiFlow\": \"https://hp-thingsboard.s3.amazonaws.com/PiFlow/\",\n",
    "        \"ipp\": \"https://hp-thingsboard.s3.amazonaws.com/ipp/\",\n",
    "        \"plcpond\": \"https://hp-thingsboard.s3.amazonaws.com/plcpond/\",\n",
    "        \"multisensor\": \"https://hp-thingsboard.s3.amazonaws.com/multisensor/\",\n",
    "        \"dualactuator\": \"https://hp-thingsboard.s3.amazonaws.com/dualactuator/\",\n",
    "        \"dualactuatorpri\": \"https://hp-thingsboard.s3.amazonaws.com/dualactuatorpri/\",\n",
    "        \"plcfreshwater\": \"https://hp-thingsboard.s3.amazonaws.com/plcfreshwater/\",\n",
    "        \"pondlevel\": \"https://hp-thingsboard.s3.amazonaws.com/pondlevel/\",\n",
    "        \"promagmbs\": \"https://hp-thingsboard.s3.amazonaws.com/promagmbs/\",\n",
    "        \"poc\": \"https://hp-thingsboard.s3.amazonaws.com/poc/\",\n",
    "        \"recycle_train\": \"https://hp-thingsboard.s3.amazonaws.com/recycle_train/\",\n",
    "        \"rigpump\": \"https://hp-thingsboard.s3.amazonaws.com/rigpump/\",\n",
    "        \"submonitor\": \"https://hp-thingsboard.s3.amazonaws.com/submonitor/\",\n",
    "        \"swdcontroller\": \"https://hp-thingsboard.s3.amazonaws.com/swdcontroller/\",\n",
    "        \"tankalarms\": \"https://hp-thingsboard.s3.amazonaws.com/tankalarms/\",\n",
    "        \"tanktransfer\": \"https://hp-thingsboard.s3.amazonaws.com/tanktransfer/\",\n",
    "        \"tenflowmeterskid\": \"https://hp-thingsboard.s3.amazonaws.com/tenflowmeterskid/\",\n",
    "        \"transferlite\": \"https://hp-thingsboard.s3.amazonaws.com/transferlite/\",\n",
    "        \"m1\": \"https://hp-thingsboard.s3.amazonaws.com/m1/\"\n",
    "    }\n",
    "    config = json.loads(config.replace(\"'\", '\"'))\n",
    "    for x in dict.keys(config):\n",
    "        if x == \"mainHP\":\n",
    "            config[x] = mainHPMapping.get(config[x].split(\"/\")[-2], \"bad_request\")\n",
    "        else:\n",
    "            config[x] = configMapping.get(x, \"bad_request\")\n",
    "    config = json.dumps(config).replace('\"', \"'\")\n",
    "    print(config)\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def updateConfig(mac, client, table):\n",
    "    resp = client.get_item(Key={'mac': {'S': mac}}, TableName=table)\n",
    "    oldconfig = resp[\"Item\"]['config']\n",
    "    print(oldconfig)\n",
    "    urls = convertToNewConfig(oldconfig)\n",
    "    newconfig = oldconfig.copy()\n",
    "    newconfig['S'] = urls\n",
    "    print(newconfig)\n",
    "    client.update_item(\n",
    "        TableName=table,\n",
    "        Key={'mac': {'S': mac}},\n",
    "        ExpressionAttributeNames={\"#C\": 'config'},\n",
    "        ExpressionAttributeValues={':c': newconfig},\n",
    "        ReturnValues='ALL_NEW',\n",
    "        UpdateExpression='SET #C = :c'\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = boto3.client('dynamodb')\n",
    "table = \"HPDeviceList\"\n",
    "macs = ['C4:93:00:0C:68:F9']\n",
    "for mac in macs:\n",
    "    updateConfig(mac, client, table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertVanityToClientId(deviceName):\n",
    "    #Device Name form: Jones Holton FW #2\n",
    "    #MQTT clientID form: jones-holton-fw-2\n",
    "    mqttClientId = []\n",
    "    for c in deviceName:\n",
    "        if c == \" \":\n",
    "            mqttClientId.append(\"-\")\n",
    "        elif c.isalnum():\n",
    "            mqttClientId.append(c.lower())\n",
    "        elif c == '\"':\n",
    "            mqttClientId.append(\"in\")\n",
    "        elif c == '-':\n",
    "            mqttClientId.append(c)\n",
    "    mqttClientId = \"\".join(mqttClientId)\n",
    "    return mqttClientId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "nodeTypes = {\n",
    "    \"tankalarms\": 95\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"id\": 73138,\n",
      "    \"parentNodeId\": 73138,\n",
      "    \"aliasNodeIds\": [],\n",
      "    \"folderId\": 35662,\n",
      "    \"nodeTypeId\": 95,\n",
      "    \"uniqueId\": \"b8:27:eb:3d:e9:11:01:99\",\n",
      "    \"vanity\": \"Fasken AW Battery Tank Alarms\",\n",
      "    \"tags\": [],\n",
      "    \"location\": null,\n",
      "    \"metadata\": {},\n",
      "    \"isActive\": true,\n",
      "    \"archiveNumber\": 0,\n",
      "    \"createdAt\": \"2021-08-19T17:54:44Z\",\n",
      "    \"updatedAt\": \"2021-08-19T17:54:44Z\",\n",
      "    \"folderName\": \"Fasken AW Battery\"\n",
      "  },\n",
      "  {\n",
      "    \"id\": 79215,\n",
      "    \"parentNodeId\": 79215,\n",
      "    \"aliasNodeIds\": [],\n",
      "    \"folderId\": 36624,\n",
      "    \"nodeTypeId\": 95,\n",
      "    \"uniqueId\": \"b8:27:eb:26:55:c0:01:99\",\n",
      "    \"vanity\": \"Fee BM Tank Alarms\",\n",
      "    \"tags\": [],\n",
      "    \"location\": null,\n",
      "    \"metadata\": {},\n",
      "    \"isActive\": true,\n",
      "    \"archiveNumber\": 0,\n",
      "    \"createdAt\": \"2022-03-17T23:01:35Z\",\n",
      "    \"updatedAt\": \"2022-03-17T23:01:35Z\",\n",
      "    \"folderName\": \"Fee BM\"\n",
      "  }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "#Get nodes and filter by node type\n",
    "nodes = lattice.getNodes()\n",
    "folders = lattice.getFolders()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fasken AW Battery fasken-aw-battery\n",
      "Fee BM fee-bm\n"
     ]
    }
   ],
   "source": [
    "desiredNodes = []\n",
    "desiredType = nodeTypes[\"tankalarms\"]\n",
    "for node in nodes:\n",
    "    if node[\"nodeTypeId\"] == desiredType:\n",
    "        for folder in folders:\n",
    "            if folder[\"id\"] == node[\"folderId\"]:\n",
    "                node[\"folderName\"] = folder[\"name\"]\n",
    "                break\n",
    "        desiredNodes.append(node)\n",
    "\n",
    "#print(json.dumps(desiredNodes, indent=2))\n",
    "for n in desiredNodes:\n",
    "    print(n[\"folderName\"],convertVanityToClientId(n[\"folderName\"]))\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "aws",
   "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.11.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
