{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/dev/ttyUSB0  /dev/ttyUSB1\r\n"
     ]
    }
   ],
   "source": [
    "!ls /dev/ttyUSB*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sys, re\n",
    "\n",
    "# https://github.com/fzinfz/meter-dlt645\n",
    "sys.path.append(\"/data/powermeter_dlt645/meter-dlt645\")\n",
    "\n",
    "from test_dlt645 import *\n",
    "import test_dlt645\n",
    "\n",
    "# https://github.com/fzinfz/flask-DLT645\n",
    "sys.path.append(\"/data/flask-DLT645/lib\")\n",
    "\n",
    "from read import *\n",
    "from write import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chn=dlt645.Channel(port_id = '/dev/ttyUSB1', tmo_cnt = 10, wait_for_read = 0.5)\n",
    "chn.open()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# auto detect address"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "--- Read meter address with broadcase mode ---\n",
      "tx_frame   : fe fe 68 aa aa aa aa aa aa 68 13 00 df 16 \n",
      "tx_addr    : aa aa aa aa aa aa \n",
      "tx_ctrl    : 13 \n",
      "tx_payload : \n",
      "wait...\n",
      "\n",
      "rx_frame   : 68 69 85 31 28 01 01 68 93 06 9c b8 64 5b 34 34 2d 16 \n",
      "rx_addr    : 01 01 28 31 85 69 \n",
      "rx_ctrl    : 93 \n",
      "rx_payload : 69 85 31 28 01 01 \n",
      "\n",
      "Meter address: 010128318569\n",
      "[1, 1, 40, 49, 133, 105]\n"
     ]
    }
   ],
   "source": [
    "read_meter_address(chn, verbose=1)\n",
    "addr = chn.rx_addr\n",
    "print(addr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'010128318569'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# algorithm for converting back to human readable\n",
    "''.join([ '{0:0>2x}'.format(s) for s in addr ])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# manually set address"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "addr = [ int(s,16) for s in re.findall('..', '001522454104') ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read meter\n",
    "restart kernel if data incorrect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'内部电池电压': (3.69, 'V'),\n",
       " '电能-组合有功总-上结算日': (0.0, 'kWh'),\n",
       " 'A相电压': (227.9, 'V'),\n",
       " 'A相电流': (0.357, 'A'),\n",
       " '功率-一分钟有功总平均': ('48.00', 'W'),\n",
       " '功率因数-总': (0.575, ''),\n",
       " '功率-瞬时总有功': ('46.80', 'W'),\n",
       " '电能-组合有功总-当前': (0.52, 'kWh'),\n",
       " '功率-A相': ('81.36', 'W'),\n",
       " '电能:本周期': ('0.52', 'kWh'),\n",
       " '日期时间': ('20201129 20:43:33', '')}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = Meter(chn, addr, level=4, verbose=0)\n",
    "m.read_meter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tx_frame   : fe fe 68 69 85 31 28 01 01 68 11 04 33 33 33 33 fa 16 \n",
      "tx_addr    : 01 01 28 31 85 69 \n",
      "tx_ctrl    : 11 \n",
      "tx_payload : 00 00 00 00 \n",
      "wait...\n",
      "\n",
      "rx_frame   : 68 69 85 31 28 01 01 68 91 08 33 33 33 33 85 33 33 33 9c 16 \n",
      "rx_addr    : 01 01 28 31 85 69 \n",
      "rx_ctrl    : 91 \n",
      "rx_payload : 00 00 00 00 52 00 00 00 \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'电能-组合有功总-当前': (0.52, 'kWh')}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = Meter(chn, addr, level=1, verbose=1)\n",
    "m.read_meter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 0, 0, 0]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "passwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "--- Read last outage timestamp of N = 1 ---\n",
      "Last outage timestamp: 20-11-28 00:27:31 ----> 20-11-28 00:50:26 \n",
      "\n",
      "--- Read energy (Current Month) ---\n",
      "Total: 0.52 kWh\n",
      "\n",
      "--- Read energy (Current Month) ---\n",
      "Peak: 0.35 kWh\n",
      "\n",
      "--- Read energy (Current Month) ---\n",
      "Trough: 0.17 kWh\n",
      "\n",
      "--- Read energy (Last 1 Month) ---\n",
      "Total: 0.00 kWh\n",
      "\n",
      "--- Read energy (Last 1 Month) ---\n",
      "Peak: 0.00 kWh\n",
      "\n",
      "--- Read energy (Last 1 Month) ---\n",
      "Trough: 0.00 kWh\n"
     ]
    }
   ],
   "source": [
    "# re-run if IndexError\n",
    "\n",
    "verbose=0\n",
    "read_last_outage_timestamp(chn, addr, 1, verbose)\n",
    "\n",
    "for m in range(0,2):\n",
    "    for n in [0,2,4]:\n",
    "        read_energy(chn, addr, m, n, verbose)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# change Date & Time\n",
    "https://github.com/glx-technologies/meter-dlt645  \n",
    "运行前开前挡板按编程键\n",
    "\n",
    "    rx_ctrl    : \n",
    "        94  => success\n",
    "        d4  => fail\n",
    "        91  => read success"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020年 11月 01日 星期日 12:37:15 CST\r\n"
     ]
    }
   ],
   "source": [
    "!date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201129\n",
      "[32, 17, 41]\n",
      "tx_frame   : fe fe 68 04 41 45 22 15 00 68 14 10 34 34 33 37 37 33 33 33 33 33 33 33 33 5c 44 53 49 16 \n",
      "tx_addr    : 00 15 22 45 41 04 \n",
      "tx_ctrl    : 14 \n",
      "tx_payload : 01 01 00 04 04 00 00 00 00 00 00 00 00 29 11 20 \n",
      "wait...\n",
      "\n",
      "rx_frame   : 68 04 41 45 22 15 00 68 91 08 34 37 33 33 7b 34 33 33 10 16 \n",
      "rx_addr    : 00 15 22 45 41 04 \n",
      "rx_ctrl    : 91 \n",
      "rx_payload : 01 04 00 00 48 01 00 00 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "change_date(chn, addr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 17, 17, 17]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "passwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "193241\n",
      "[25, 50, 65]\n",
      "tx_frame   : fe fe 68 69 85 31 28 01 01 68 14 0f 35 34 33 37 37 33 33 33 33 33 33 33 74 65 4c d0 16 \n",
      "tx_addr    : 01 01 28 31 85 69 \n",
      "tx_ctrl    : 14 \n",
      "tx_payload : 02 01 00 04 04 00 00 00 00 00 00 00 41 32 19 \n",
      "wait...\n",
      "\n",
      "rx_frame   : 68 69 85 31 28 01 01 68 d4 01 37 25 16 \n",
      "rx_addr    : 01 01 28 31 85 69 \n",
      "rx_ctrl    : d4 \n",
      "rx_payload : 04 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "change_time(chn, addr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# close channel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "chn.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}