XPadApi.java 80.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264
package com.sunvote.xpadcomm;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import com.sunvote.util.ByteUtils;
import com.sunvote.util.LogUtil;
import com.sunvote.xpadcomm.usb.UsbTransferManager;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class XPadApi implements XPadApiInterface {
    private static String TAG = "XPadApi";
    private ComListener m_listener;
    private static XPadApi m_xpadApi = null;

    private OutputStream mOutputStream;
    private InputStream mInputStream;
    private ReadThread mReadThread;
    private List<VoteResultItem> sendQueen = Collections.synchronizedList(new ArrayList<VoteResultItem>());//new ArrayList<VoteResultItem>();
    private volatile int serialNumber;
    private int allOkSerialNumber;
    private int serviceSerialNumber;

    private boolean isShowLog = true;
    private boolean isShowOnlineLog = false;

    private OnLineInfo onLineInfo = new OnLineInfo();
    private KeypadInfo info = new KeypadInfo();
    private HandlerThread receiverThread = new HandlerThread("rea");
    private HandlerThread sendThread = new HandlerThread("send");
    private Handler taskHandler = null;
    private byte[] baseSign ;
    private byte[] voteSign ;

    // private boolean hasGetBaseInfo = false;

    private int writeFrimPageErrCnt = 0;

    // public SerialPortFinder mSerialPortFinder = new SerialPortFinder();



    private XPadApi(){
        sendThread.start();
        taskHandler = new Handler(sendThread.getLooper());
    }

    public synchronized static XPadApi getInstance() {
        if (m_xpadApi == null) {
            m_xpadApi = new XPadApi();
            m_xpadApi.init();
        }
        return m_xpadApi;
    }

    public void init() {
        try {
            LogUtil.d(TAG, "XPadApi.init()");
            allOkSerialNumber = -1;
            serviceSerialNumber = -1;
//            mSerialPort = getSerialPort();
//            module = new UDPModule();
//            mOutputStream = module.getOutputStream();
//            mInputStream = module.getInputStream();
//            mOutputStream = mSerialPort.getOutputStream();
//            mInputStream = mSerialPort.getInputStream();
            UsbTransferManager.getInstance().setOnUsbConnectListener(new UsbTransferManager.OnUsbConnectListener() {
                @Override
                public boolean onConnect(boolean isConnected) {
                    if(isConnected){
                        byte[] mBuffer = new byte[0x1F + 4];
                        Arrays.fill(mBuffer, (byte) 0x0);
                        mBuffer[0] = (byte) 0xF5;
                        mBuffer[1] = (byte) 0xAA;
                        mBuffer[2] = (byte) 0xAA;
                        mBuffer[3] = (byte) 0x1F;

                        mBuffer[4] = 0x70;
                        mBuffer[5] = 0x03;
                        writeToCom(mBuffer);
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    byte[] mBuffer = new byte[0x1F + 4];
                    Arrays.fill(mBuffer, (byte) 0x0);
                    mBuffer[0] = (byte) 0xF5;
                    mBuffer[1] = (byte) 0xAA;
                    mBuffer[2] = (byte) 0xAA;
                    mBuffer[3] = (byte) 0x1F;

                    mBuffer[4] = 0x70;
                    mBuffer[5] = 0x04;
                    writeToCom(mBuffer);
                    return true;
                }
            });
            mOutputStream = UsbTransferManager.getInstance().getOutputStream();
            mInputStream = UsbTransferManager.getInstance().getInputStream();

            Arrays.fill(broadcastData, (byte) 0x0);// 清空多包结果

			/* Create a receiving thread */
            mReadThread = new ReadThread();
            mReadThread.start();

            Log.d(TAG, "XPadApi init");
//			InitThread initThread = new InitThread();
//			initThread.start();

        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class InitThread extends Thread {
        @Override
        public void run() {
            try {
                sleep(100);
                getKeypadParam();// 唤醒
                sleep(100);
                getKeypadParam();
                sleep(100);
                getWorkMode();

            } catch (Exception e) {
                // TODO: handle exception
            }
            super.run();
        }
    }



    public void closeCom() {
        if (mReadThread != null)
            mReadThread.interrupt();


//        if(module != null){
//            module.release();
//        }

//        if (mSerialPort != null) {
//            mSerialPort.close();
//            mSerialPort = null;
//        }     
    }

    public static void printDataBuf(byte[] buf, int len, String flag) {
        String tmpStr = new String();
        for (int i = 0; i < len; i++) {
            tmpStr += String.format("%x ", buf[i]);
        }
        LogUtil.d(TAG, flag + ":" + tmpStr);
    }

    public static String getDataBufString(byte[] buf, int len, String flag) {
        String tmpStr = new String();
        for (int i = 0; i < len; i++) {
            tmpStr += String.format("%x ", buf[i]);
        }
        return flag + ":" + tmpStr;
    }

    public static byte[] intToByteArray1(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }

    @Override
    public void setComListener(ComListener cl) {
        m_listener = cl;

    }

    public class VoteResultItem {
        public int serialNo;
        public int status;// 0:未发送   1:已发送
        public int ansType; //
        public int ansCount;// 批次提交的选项数
        public int allOK; //1 确认提交
        public byte[] data;
        public boolean sendOk;
        public int sendTimes = 0;
        /**
         * 用于回调标志结果显示
         */
        public String retBack = "" ;
    }

    private Runnable sendTask = new Runnable() {
        @Override
        public void run() {
            // 从队列中取出待发送数据
            VoteResultItem voteResultItem = null;
            boolean send = false;
            synchronized (sendQueen) {
                if(sendQueen.size() > 0) {
                    Iterator<VoteResultItem> it = sendQueen.iterator();
                    while (it.hasNext()) {
                        voteResultItem = it.next();
                        if ((!voteResultItem.sendOk && !send) || voteResultItem.ansType == AnsType_Select) {
                            // 取出数据发送
                            if(send){
                                try {
                                    Thread.sleep(50);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            voteResultItem.status = 1 ;
                            if(voteResultItem.sendTimes < 1){
                                writeToCom(voteResultItem.data);
                            }
                            voteResultItem.sendTimes++;
                            if(voteResultItem.sendTimes > 6){
                                voteResultItem.sendTimes = 0 ;
                            }
                            send = true;
                        }
                    }
                }
            }
            // 轮询下一次发送数据
            taskHandler.removeCallbacks(this);
            taskHandler.postDelayed(this,1000);
        }
    };

    /*
     * function:addToSendQueen params: 发送内容 功能:添加到发送队列
     */
    private void addToSendQueen(VoteResultItem item) {
        boolean isComb = false;// 合并
        synchronized (sendQueen) {
            if (item.ansType == AnsType_BatchSingle && item.allOK != 1) {
                for (int i = 0; i < sendQueen.size(); i++) {
                    VoteResultItem it = (VoteResultItem) sendQueen.get(i);
                    if (it.status != 1 && it.ansType == AnsType_BatchSingle && it.ansCount < 6) {
                        it.data[9 + it.ansCount * 3] = item.data[9];
                        it.data[10 + it.ansCount * 3] = item.data[10];
                        it.data[11 + it.ansCount * 3] = item.data[11];
                        it.ansCount++;
                        isComb = true;
                        LogUtil.d(TAG, "addToSendQueen  isComb=true 合并:" + it.ansCount);
                        break;
                    }
                }
            }
        }

        if (isComb == false) {
            sendQueen.add(item);
        }
        LogUtil.d(TAG, "addToSendQueen serialNo=" + item.serialNo + "  size=" + sendQueen.size());
        // 轮询下一次发送数据
        taskHandler.removeCallbacks(sendTask);
        taskHandler.post(sendTask);
    }

    private void sendToModalSuccessResponse(int serialNo) {
        LogUtil.d(TAG,"sendToModalSuccessResponse:" + serialNo);
        synchronized (sendQueen) {
            Iterator<VoteResultItem> it = sendQueen.iterator();
            while (it.hasNext()) {
                VoteResultItem voteResultItem = it.next();
                if (voteResultItem.status == 1 && serialNo == voteResultItem.serialNo) {
                    voteResultItem.sendOk = true;
                    LogUtil.d(TAG, "set send ok sucess :" + serialNo);
                    break;
                }
            }
        }
    }

    /*
     * function:addToSendQueen params:流水号 功能:根据流水号删除队列已发送成功的项
     */
    private VoteResultItem removeSentItem(int serialNo) {
        LogUtil.d(TAG, "send success : " + serialNo);
        VoteResultItem ret = null;
        synchronized (sendQueen) {
            Iterator<VoteResultItem> it = sendQueen.iterator();
            while (it.hasNext()) {
                VoteResultItem voteResultItem = it.next();
                if (voteResultItem.status == 1 && voteResultItem.serialNo == serialNo) {
                    voteResultItem.sendOk = true;
                    boolean remove = sendQueen.remove(voteResultItem);
                    LogUtil.d(TAG, "remove success ? " + remove);
                    ret = voteResultItem;
                    break;
                }
            }
        }
        return ret;
    }

    private void clearSentItems() {
        sendQueen.clear();
    }

    @Override
    public void getWorkMode() {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x01;

        writeToCom(mBuffer);
    }

    @Override
    public void setWorkMode(int iMode) {

        byte[] mBuffer = new byte[0x1F + 4];
        // Arrays.fill(mBuffer, (byte) 0x55);
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x02;
        mBuffer[6] = (byte) iMode;

        writeToCom(mBuffer);
    }

    @Override
    public void getBaseStatus() {
        if(baseSign != null){
            onBaseInfo(baseSign);
        }
       /* byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x03;
        writeToCom(mBuffer);*/
    }

    @Override
    public void getVoteStatus() {
        if(voteSign != null){
            onVoteInfo(voteSign);
        }
        /*byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x04;
        writeToCom(mBuffer);*/


    }

    @Override
    public void getKeypadParam() {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x05;
        writeToCom(mBuffer);
    }

    @Override
    public void setKeypadParam(int keyId, byte[] KEYSN) {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x06;

        mBuffer[6] = (byte) ((keyId >> 8) & 0xFF);// keyId
        mBuffer[7] = (byte) (keyId & 0xFF);

        // mBuffer[8] =(byte) 0xFF;
        System.arraycopy(KEYSN, 0, mBuffer, 8, 6);

        byte[] parecode = new byte[4];
        Arrays.fill(parecode, (byte) 0xFF);
        System.arraycopy(KEYSN, 0, mBuffer, 14, 4);

        writeToCom(mBuffer);
    }

    @Override
    public void checkOnLine(int volt, int keyinStatus) {
        LogUtil.i(TAG ,"HEART BEAT(S):" + volt + " " + System.currentTimeMillis());
        if (isInComCommunicationTest) {
            return;
        }
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x07;

        mBuffer[6] = (byte) volt;
        mBuffer[7] = (byte) keyinStatus;
        writeToCom(mBuffer);
    }

    @Override
    public void execKeypadMatch(int iMode, int channal) {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x08;

        mBuffer[6] = (byte) iMode;
        mBuffer[7] = (byte) channal;
        writeToCom(mBuffer);
    }

    private boolean isInComCommunicationTest = false;

    @Override
    public void comCommunicationTest(int sendn, int okn) {
        isInComCommunicationTest = (sendn != 200);
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x30;
        mBuffer[5] = 0x0;
        mBuffer[6] = 0x0;

        mBuffer[7] = 7;
        mBuffer[8] = (byte) sendn;
        mBuffer[9] = (byte) okn;
        mBuffer[10] = (byte) 0xAA;

        for (int i = 1; i < 17; i++) {
            mBuffer[10 + i] = (byte) i;
        }

        writeToCom(mBuffer);
    }

    @Override
    public void submitVote(int ansType, String info) {// byte[] ansData

        LogUtil.i(TAG,"ansType:" + ansType + ",info:" + info);
        if (ansType == AnsType_Single) {// 单值
            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 0x1F;

            mBuffer[4] = 0x73;
            mBuffer[5] = (byte) serialNumber;// 流水号

            mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
            mBuffer[7] = (byte) ansType;
            int val = Integer.parseInt(info);
            mBuffer[8] = (byte) val;

            VoteResultItem it = new VoteResultItem();
            it.status = 0;
            it.ansType = ansType;
            it.ansCount = 1;
            it.serialNo = serialNumber;
            serialNumberInc();
            it.data = mBuffer;
            addToSendQueen(it);
        } else if (ansType == AnsType_Select) {
            String[] ary = info.split(",");
            String val = ary[0];
            int tm = Integer.parseInt(ary[1]);

            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 0x1F;

            mBuffer[4] = 0x73;
            mBuffer[5] = (byte) serialNumber;// 流水号

            mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
            mBuffer[7] = (byte) ansType;

            ;
            mBuffer[8] = (byte) ((tm >> 8) & 0xff);
            mBuffer[9] = (byte) (tm & 0xff);

            mBuffer[10] = (byte) ((tm >> 8) & 0xff);
            mBuffer[11] = (byte) (tm & 0xff);

            VoteResultItem it = new VoteResultItem();
            it.status = 0;
            it.ansType = ansType;
            it.ansCount = 1;
            it.serialNo = serialNumber;
            serialNumberInc();
            it.data = mBuffer;
            addToSendQueen(it);
//            writeToCom(it.data);
        } else if (ansType == AnsType_Number) {
            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 0x1F;

            mBuffer[4] = 0x73;
            mBuffer[5] = (byte) serialNumber;// 流水号

            mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
            mBuffer[7] = (byte) ansType;

            byte[] val = util_encodeBCD(info.getBytes());
            System.arraycopy(val, 0, mBuffer, 8, 8);

            VoteResultItem it = new VoteResultItem();
            it.status = 0;
            it.ansType = ansType;
            it.ansCount = 1;
            it.serialNo = serialNumber;
            serialNumberInc();
            it.data = mBuffer;
            addToSendQueen(it);
        } else if (ansType == AnsType_LoginIn) {
            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 0x1F;

            mBuffer[4] = 0x73;
            mBuffer[5] = (byte) serialNumber;// 流水号

            mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
            mBuffer[7] = (byte) ansType;
            mBuffer[8] = 3;//3 签到信息按BCD码格式
            byte[] val = util_encodeBCD(info.getBytes());
            System.arraycopy(val, 0, mBuffer, 9, 9);

            VoteResultItem it = new VoteResultItem();
            it.status = 0;
            it.ansType = ansType;
            it.ansCount = 1;
            it.serialNo = serialNumber;
            serialNumberInc();
            it.data = mBuffer;
            addToSendQueen(it);

        } else if (ansType == AnsType_BatchSingle) {

            // String[] ary= info.split(",");

            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 0x1F;

            mBuffer[4] = 0x73;
            mBuffer[5] = (byte) serialNumber;// 流水号

            mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
            mBuffer[7] = (byte) ansType;
            mBuffer[8] = 0;// ALLOK
            int pos = 9;

            String[] item = info.split(":");
            if (item.length > 1) {
                int num = Integer.parseInt(item[0]);
                int val = Integer.parseInt(item[1]);
                mBuffer[pos++] = (byte) (num >> 8);
                mBuffer[pos++] = (byte) (num & 0xff);
                mBuffer[pos++] = (byte) val;
            }

            VoteResultItem it = new VoteResultItem();
            it.status = 0;
            it.ansType = ansType;
            it.serialNo = serialNumber;
            serialNumberInc();
            it.ansCount = 1;
            it.data = mBuffer;
            addToSendQueen(it);
        } else if (ansType == AnsType_BatchNumber) {
            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 0x1F;

            mBuffer[4] = 0x73;
            mBuffer[5] = (byte) serialNumber;// 流水号

            mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
            mBuffer[7] = (byte) ansType;
            mBuffer[8] = 0;// ALLOK

            String[] item = info.split(":");
            if (item.length > 1) {
                String[] itIndex = item[0].split("_");
                if (itIndex.length == 2) { //综合测评
                    int personId = Integer.parseInt(itIndex[0]);
                    int projectId = Integer.parseInt(itIndex[1]);
                    mBuffer[9] = (byte) personId;
                    mBuffer[10] = (byte) projectId;
                } else {
                    int num = Integer.parseInt(item[0]);
                    mBuffer[9] = (byte) (num >> 8);
                    mBuffer[10] = (byte) (num & 0xff);
                }
                String strNum = item[1];
                byte[] val = util_encodeBCD(strNum.getBytes());
                System.arraycopy(val, 0, mBuffer, 11, 4);
            }

            VoteResultItem it = new VoteResultItem();
            it.status = 0;
            it.ansType = ansType;
            it.serialNo = serialNumber;
            serialNumberInc();
            it.ansCount = 1;
            it.data = mBuffer;
            addToSendQueen(it);

        } else if (ansType == AnsType_SelectOther) {
            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 0x1F;

            mBuffer[4] = 0x73;
            mBuffer[5] = (byte) serialNumber;// 流水号

            mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
            mBuffer[7] = (byte) ansType;
            mBuffer[8] = 0;// ALLOK
            int pos = 9;

            String[] item = info.split(":");
            if (item.length > 1) {
                int num = Integer.parseInt(item[0]);
                String val = item[1];
                mBuffer[pos++] = (byte) (num >> 8);
                mBuffer[pos++] = (byte) num;

                mBuffer[pos++] = 0;// slot
                try {
                    byte[] name = val.getBytes("GB2312");

                    System.arraycopy(name, 0, mBuffer, pos, name.length > 16 ? 16 : name.length);

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                // 名字最多16
            }

            VoteResultItem it = new VoteResultItem();
            it.status = 0;
            it.ansType = ansType;
            it.serialNo = serialNumber;
            serialNumberInc();
            it.ansCount = 1;
            it.data = mBuffer;
            addToSendQueen(it);

        } else if (ansType == AnsType_Service) {
            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 0x1F;

            mBuffer[4] = 0x73;
            mBuffer[5] = (byte) serialNumber;// 流水号

            mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
            mBuffer[7] = (byte) ansType;
            mBuffer[8] = 4;
            int serv_type = Integer.parseInt(info);
            mBuffer[9] = (byte) serv_type;

            mBuffer[10] = (byte) serialNumber;

            VoteResultItem it = new VoteResultItem();

            serviceSerialNumber = serialNumber;

            it.status = 0;
            it.ansType = ansType;
            it.serialNo = serialNumber;
            serialNumberInc();
            it.ansCount = 1;
            it.data = mBuffer;
            addToSendQueen(it);

        }
    }

    private int ChoiceValueToInt(String val) {
        int ret = 0;
        for (int i = 0; i < val.length(); i++) {
            char cc = val.charAt(i);
            if (cc < 'I') {
                ret |= 1 << (8 + (cc - 'A'));
            } else {
                ret |= 1 << (cc - 'I');
            }

        }

        return ret;
    }

    public void submitVoteFource(int ansType, String info) {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x73;
        mBuffer[5] = (byte) serialNumber;// 流水号

        mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
        mBuffer[7] = (byte) ansType;
        mBuffer[8] = 0;// ALLOK
        int pos = 9;

        String[] item = info.split(":");
        if (item.length > 1) {
            int num = Integer.parseInt(item[0]);
            String val = item[1];
            mBuffer[pos++] = (byte) (num >> 8);
            mBuffer[pos++] = (byte) num;

            mBuffer[pos++] = 0;// slot
            try {
                byte[] name = val.getBytes("GB2312");

                System.arraycopy(name, 0, mBuffer, pos, name.length > 16 ? 16 : name.length);

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 名字最多16
        }

        VoteResultItem it = new VoteResultItem();
        it.status = 0;
        it.ansType = ansType;
        it.serialNo = serialNumber;
        serialNumberInc();
        it.ansCount = 1;
        it.data = mBuffer;
        addToSendQueen(it);
    }

    @Override
    public void submitVoteAllOK() {
        int allokSerialNum = 0xff;
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x73;
        mBuffer[5] = (byte) allokSerialNum;// 流水号

        mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
        mBuffer[7] = (byte) AnsType_BatchSingle;
        mBuffer[8] = 1;// ALLOK

        VoteResultItem it = new VoteResultItem();
        it.status = 0;
        it.ansType = AnsType_BatchSingle;
        it.serialNo = allokSerialNum;
        allOkSerialNumber = allokSerialNum;
        it.ansCount = 1;
        it.allOK = 1;
        it.data = mBuffer;
        addToSendQueen(it);

    }

    public void submitVoteAllOKWithValue(int ansType, String info) {
        LogUtil.i(TAG,"ansType:" + ansType + ",info:" + info);
        int allokSerialNum = 0xff;
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x73;
        mBuffer[5] = (byte) allokSerialNum;// 流水号

        mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
        mBuffer[7] = (byte) ansType;
        mBuffer[8] = 1;// ALLOK

        String[] item = info.split(":");
        if (item.length > 1) {
            String[] itIndex = item[0].split("_");
            if (itIndex.length == 2) { //综合测评
                int personId = Integer.parseInt(itIndex[0]);
                int projectId = Integer.parseInt(itIndex[1]);
                mBuffer[9] = (byte) personId;
                mBuffer[10] = (byte) projectId;
            } else {
                int num = Integer.parseInt(item[0]);
                mBuffer[9] = (byte) (num >> 8);
                mBuffer[10] = (byte) (num & 0xff);
            }
            String strNum = item[1];
            byte[] val = util_encodeBCD(strNum.getBytes());
            System.arraycopy(val, 0, mBuffer, 11, 4);
        }

        VoteResultItem it = new VoteResultItem();
        it.status = 0;
        it.ansType = ansType;
        it.serialNo = allokSerialNum;
        allOkSerialNumber = allokSerialNum;
        it.ansCount = 1;
        it.allOK = 1;
        it.data = mBuffer;
        addToSendQueen(it);

    }

    @Override
    public void cancelSubmitVoteAllOK() {
        int cancelAllokSerialNum = 0xfe;
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x73;
        mBuffer[5] = (byte) cancelAllokSerialNum;// 流水号

        mBuffer[6] = 1;// MSGTYPE 1:ID mode, 2:SN
        mBuffer[7] = (byte) AnsType_BatchSingle;
        mBuffer[8] = 0;// ALLOK

        VoteResultItem it = new VoteResultItem();
        it.status = 0;
        it.ansType = AnsType_BatchSingle;
        it.serialNo = cancelAllokSerialNum;

        it.ansCount = 1;
        it.allOK = 0;
        it.data = mBuffer;
        addToSendQueen(it);

        //serialNumber=0;
    }

    private void serialNumberInc() {
        serialNumber++;
        if (serialNumber == 0xfe) {
            serialNumber = 0;
        }
    }

    @Override
    public void submitSelectOther(String info) {
        // TODO Auto-generated method stub

    }

    @Override
    public void submitVoteBySn(byte[] keySn, String info) {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x73;
        mBuffer[5] = 0x08;// 流水号

        mBuffer[6] = 2;// MSGTYPE

        mBuffer[7] = 1;// ANSTYPE

        System.arraycopy(keySn, 0, mBuffer, 8, 6);// keysn
        // System.arraycopy(ansData, 0, mBuffer, 8,
        // ansData.length<=16?ansData.length:16);
        // info???
        writeToCom(mBuffer);
    }

    @Override
    public void sendCmdData(int cmdId, byte[] data) {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = (byte) 0xB0;// 0x30 表决器下载单包类指令

        mBuffer[5] = 0x0;// keyid 填 0
        mBuffer[6] = 0x0;// keyid

        mBuffer[7] = (byte) cmdId;// KCMD
        System.arraycopy(data, 0, mBuffer, 8, data.length);// keysn

        writeToCom(mBuffer);
    }

    @Override
    public void configMode() {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x70;
        mBuffer[5] = 0x09;

        writeToCom(mBuffer);

    }

    private byte[] firmFileBuffer;
    private int firmWritePage;
    private int firmWritePageMax;

    @Override
    public void startFirmUpdate(byte[] fileBuffer) {
        firmFileBuffer = fileBuffer;
        compareCount = 0 ;
        taskHandler.post(updateTask);
    }

    private void keepFirmMode() {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 10;

        mBuffer[4] = 0x78;
        mBuffer[5] = 0x02;

        writeToCom(mBuffer);
    }

    private void eraseFlash() {
        int file_len = firmFileBuffer.length;
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 10;
        mBuffer[4] = 0x78;
        mBuffer[5] = 0x03;
        mBuffer[6] = 0;
        mBuffer[7] = (byte) (file_len / 1024);
        writeToCom(mBuffer);

    }

    private Runnable updateTask = new Runnable() {
        @Override
        public void run() {
            firmWritePage = 0;
            firmWritePageMax = firmFileBuffer.length / 32;
            if (firmWritePageMax % 32 > 0) {
                firmWritePageMax++;
            }

            byte[] mBuffer = new byte[0x1F + 4];
            Arrays.fill(mBuffer, (byte) 0x0);
            mBuffer[0] = (byte) 0xF5;
            mBuffer[1] = (byte) 0xAA;
            mBuffer[2] = (byte) 0xAA;
            mBuffer[3] = (byte) 10;
            mBuffer[4] = 0x78;
            mBuffer[5] = 0x01;
            writeToCom(mBuffer);
            if(compareCount < 20){
                compareCount++;
                taskHandler.postDelayed(this,1000);
            }
        }
    };

    private void writeFlash(int page) {
        byte[] mBuffer = new byte[38 + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 38;

        mBuffer[4] = 0x78;
        mBuffer[5] = 0x04;

        mBuffer[6] = (byte) (page / 256);
        mBuffer[7] = (byte) (page % 256);
        int pos = 0;
        for (int i = 0; i < 32; i++) {
            pos = page * 32 + i;
            if (pos == firmFileBuffer.length - 1) {
                break;
            }
            mBuffer[8 + i] = firmFileBuffer[pos];
        }
        LogUtil.d(TAG, "write flash page:" + page);
        writeToCom(mBuffer);
    }

    /*
     * status 1: success 2 : fail
     */
    private void exitFirmUpdate(int status) {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 10;

        mBuffer[4] = 0x78;
        mBuffer[5] = 5;
        mBuffer[6] = (byte) status;
        writeToCom(mBuffer);
    }

    private void writeToCom(byte[] data) {
        if (mOutputStream != null) {
            try {
                int crcValue = Crc16.getUnsignedShort(Crc16.crc16(data, data.length - 4 - 2));
                data[data.length - 2] = (byte) (crcValue >> 8);
                data[data.length - 1] = (byte) (crcValue);

                if (isShowLog) {
                    if (data[4] == 0x70 && data[5] == 0x07) {
                        // LogUtil.d(TAG, "check online");
                        if (isShowOnlineLog) {
                            printDataBuf(data, data.length, "send:");
                        }
                    } else {
                        printDataBuf(data, data.length, "send:");
                    }
                }

                mOutputStream.write(data);
            } catch (IOException e) {
                e.printStackTrace();
            }

            m_listener.onSendData(data, data.length);
        }
    }

    private byte[] SerDataRx = new byte[1024];
    int iSerRxN = 0;
    private byte[] comBuffer = new byte[1024];

    private class ReadThread extends Thread {

        @Override
        public void run() {
            super.run();
            LogUtil.d(TAG, "Xpad ReadThread running ");
            if (mInputStream != null) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] bytes = new byte[1024];
                int tmpLength = -1;
                while (!isInterrupted()) {
                    try {
//                        tmpLength = mInputStream.read(bytes);
                        bytes = UsbTransferManager.getInstance().receiveUsbRequestData();
                        if(bytes == null){
                            continue;
                        }
                        tmpLength = bytes.length;
                        if (tmpLength < 0) {
                            break;
                        }
                        // 1. 先检测读取数据到缓冲区
                        byteArrayOutputStream.write(bytes, 0, tmpLength);
                        // 2. 判断缓冲区的数据标志是否存在
                        if (byteArrayOutputStream.size() >= 3) {
                            byte[] temp = byteArrayOutputStream.toByteArray();
                            int find = ByteUtils.findBytes(temp, new byte[]{(byte) 0xF5, (byte) 0xAA, (byte) 0xAA}, 0);
                            if (find >= 0) {
                                // 3. 标志存在,则继续读取长度
                                if (byteArrayOutputStream.size() >= find + 4) {
                                    int length = ByteUtils.byte1ToInt(byteArrayOutputStream.toByteArray()[find + 3]);
                                    if (byteArrayOutputStream.size() >= find + 4 + length) {
                                        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
                                        // 4. 根据长度读取包的内容
                                        for (int i = find; i < find + 4 + length; i++) {
                                            // 5. 截取包的内容,向外抛出,处理,接着读取下一个包
                                            tmp.write(temp[i]);
                                        }

                                        //CRC 校验
                                        byte[] datas = tmp.toByteArray();
                                        LogUtil.i(TAG, "RECEIVER DATA:", datas);

                                        try {
                                            //处理
                                            if(Crc16.checkPack(datas)){
                                                int crc = Crc16.getUnsignedShort(Crc16.crc16(datas,length - 2));
                                                int originCrcValue = 0;
                                                originCrcValue |= (datas[length + 2] & 0xff) << 8;
                                                originCrcValue |= datas[length + 3] & 0xff;
                                                if(crc == originCrcValue){
                                                    checkComData(datas, tmp.size());
                                                }
                                            }
                                        } catch (Exception ex) {
                                            LogUtil.i(TAG, "处理命令出错:", ex);
                                        }
                                        // 7. 剩余数据重新打包处理
//                                        byteArrayOutputStream = new ByteArrayOutputStream();
                                        byteArrayOutputStream.reset();
                                        for (int i = find + 4 + length; i < temp.length; i++) {
                                            byteArrayOutputStream.write(temp[i]);
                                        }
                                    }
                                }
                            }
                        }
                        if (byteArrayOutputStream.size() > 2048) {
                            LogUtil.i(TAG, "被恶意攻击?或者传输数据错误?丢弃处理!", byteArrayOutputStream.toByteArray());
                            byteArrayOutputStream.reset();
                        }
                    } catch (Exception e) {
                        LogUtil.e(TAG, e);
                    }
                }
            }
        }
        @SuppressLint("NewApi")
        private void checkComData(byte[] data, int size) {
            m_listener.onComData(data, size);
            int cmd = data[4] & 0xFF;
            switch (cmd) {
                case CMD_CHECK_BASE_STATUS_RESPONSE:// 查询状态类指令应答
                    int cmd1 = data[5] & 0xFF;
                    if (cmd1 == 1) {// 返回当前工作模式和版本
                        ModelInfo info = new ModelInfo();
                        info.mode = data[6];
                        info.hModel = data[7] & 0xff;
                        info.sVer = data[8] + "." + data[9] + "." + data[10];
                        m_listener.onModelEvent(info);
                    } else if (cmd1 == 3) { // 查询基础信标 结果
                        onBaseInfo(data);
                    } else if (cmd1 == 4) { // 查询投票信标 结果
                        onVoteInfo(data);
                    } else if (cmd1 == 5 || cmd1 == 6) {// 返回当前键盘参数结果
                        info.ok = data[6];
                        info.chan = data[7];
                        info.keyId = ((data[8] & 0xff) << 8) | (data[9] & 0xff);
                        byte[] sn = Arrays.copyOfRange(data, 10, 16);
                        info.keySn = getKeySn(sn);
                        byte[] mc = Arrays.copyOfRange(data, 16, 19);
                        info.matchCode = new String(mc);
                        m_listener.onKeyPadEvent(info);

                        // if(!hasGetBaseInfo){ //第一次启动时取
                        // hasGetBaseInfo = true;
                        // getBaseStatus();
                        // }

                    } else if (cmd1 == 7) {// 查询键盘在线状态结果
                        OnLineInfo info = new OnLineInfo();
                        info.onLine = data[6];
                        info.idMode = data[7];
                        info.chan = data[8] & 0xff;
                        info.rssi = data[9] & 0xff;
                        LogUtil.i(TAG ,"HEART BEAT(R):" + info.chan + " " + System.currentTimeMillis());
                        info.tx = data[10];
                        info.rx = data[11];
                        info.baseId = data[12] & 0xff;
                        info.keyId = ((data[13] & 0xff) << 8) | (data[14] & 0xff);
                        RFFileUploadModule.getInstance().setKeyid(data,13);
                        byte[] sn = Arrays.copyOfRange(data, 15, 21);
                        info.keySn = getKeySn(sn);
                        m_listener.onOnLineEvent(info);
                        onLineInfo = info;

                    } else if (cmd1 == 8 || cmd1 == 9) {
                        info.cmd1 = cmd1;
                        info.ok = data[6];
                        info.chan = data[7];
                        info.keyId = ((data[8] & 0xff) << 8) | (data[9] & 0xff);
                        byte[] sn = Arrays.copyOfRange(data, 10, 16);
                        info.keySn = getKeySn(sn);
                        byte[] mc = Arrays.copyOfRange(data, 16, 19);
                        info.matchCode = new String(mc);
                        m_listener.onKeyPadEvent(info);
                    }

                    break;
                case CMD_BASE_STATUS_CHANGE:// 基础信标变化(主动下发)

                    responseBaseStatusChange(data, size);

                    onBaseInfo(data);
                    break;
                case CMD_VOTE_STATUS_CHANGE:// 投票信标变化
                    responseVoteStatusChange(data, size);
                    clearSentItems();
                    onVoteInfo(data);
                    break;

                case CMD_UPLOAD_DATA_RESPONSE:
                    // 上传文件指令
                    onMutilPacketRequest(data, size);
                    break;
                case CMD_VOTE_RESULT_SEND_RESPONSE:
//                    sendToModalSuccessResponse(data[5] & 0xff);
                    break;
                case CMD_VOTE_SEND_SUCCESS_RESPONSE:
                    responseVoteSendSuccess(data, size);
                    break;

                case CMD_KEY_MANAGE:// 表决器管理类指令,单包,透传
                    CmdDataInfo info = new CmdDataInfo();
                    info.cmd = data[7];
                    info.data = Arrays.copyOfRange(data, 8, 28);
                    if (info.cmd == 25) {
                        int keyId = (data[5] << 8) | data[6];
                        if (keyId != 0) {
                            responseClearKeyboardData(data, size);
                        }
                    } else if (info.cmd == 20) {
                        int keyId = (data[5] << 8) | data[6];
                        if (keyId == onLineInfo.keyId) {
                            responseAuthrizeKeyboardData(data, size);
                        } else {
                            break;
                        }
                    }else if(info.cmd == 50){
                        responseAuthrizeKeyboardData(data,size);
                    }
                    m_listener.onCmdData(info);
                    break;
                case CMD_MULTI_PCKAGE_DOWNLOAD:
                    onMultiPackageInfo(data, size);
                    break;
                case CMD_COM_COMMUNICATION_TEST_RESPONSE:
                    int sendn = data[8] & 0xff;
                    boolean isOk = true;
                    for (int i = 1; i < 17; i++) {
                        if (data[10 + i] != i) {
                            isOk = false;
                        }
                    }
                    m_listener.onComCommunicationTest(sendn, isOk);
                    break;
                case CMD_FIRM_UPDATE_RESPONSE:
                    taskHandler.removeCallbacks(updateTask);
                    switch (data[5]) {
                        case 2:// '模块询问是否进入DFU
                            keepFirmMode();
                            try{
                                Thread.sleep(1000);
                            }catch (Exception ex){}
                            eraseFlash();
                            break;
                        case 3:// 擦除应答
                            if (data[8] != 1) {
                                LogUtil.e(TAG, "eraseFlash fail");
                                m_listener.onFirmUpdateResult(false, "擦除固件失败!");
                                break;
                            }
                            LogUtil.e(TAG, "eraseFlash ok");
                            m_listener.onFirmUpdateInfo("擦除固件完成,开始写入...");
                            CheckFirmReceveThread checkFirmThread = new CheckFirmReceveThread();
                            checkFirmThread.start();
                            writeFrimPageErrCnt = 0;
                            writeFlash(firmWritePage);
                            break;
                        case 4:// 写应答
                            if (data[8] == 1) {
                                firmWritePage++;
                                writeFrimPageErrCnt = 0;
                                if (firmWritePage < firmWritePageMax) {
                                    m_listener.onFirmUpdate(firmWritePage * 100 / firmWritePageMax);
                                    LogUtil.d(TAG, "write page ok! " + firmWritePage + "/" + firmWritePageMax + "  =="
                                            + firmWritePage * 100 / firmWritePageMax + "%");
                                    //sleep(500);
                                    writeFlash(firmWritePage);
                                } else {
                                    m_listener.onFirmUpdateInfo("写入完成");
                                    m_listener.onFirmUpdate(100);
                                    LogUtil.i(TAG, "write 100%");
                                    sleep(2000);
                                    exitFirmUpdate(1);
                                    LogUtil.i(TAG, "exitFirmUpdate");
                                    m_listener.onFirmUpdateResult(true, null);
                                }
                            } else {
                                LogUtil.d(TAG, "应答写失败 write page error");
                            }
                            break;
                        case 5: // 退出升级
                            LogUtil.d(TAG, "exit update!");
                            m_listener.onFirmUpdateInfo("退出升级模式成功!");
                            sleep(3000);
                            LogUtil.d(TAG, "sleep 3s getWorkMode!");
                            for (int i = 0; i < 5; i++) {
                                getWorkMode();
                                sleep(1000);
                            }
                            byte[] mBuffer = new byte[0x1F + 4];
                            Arrays.fill(mBuffer, (byte) 0x0);
                            mBuffer[0] = (byte) 0xF5;
                            mBuffer[1] = (byte) 0xAA;
                            mBuffer[2] = (byte) 0xAA;
                            mBuffer[3] = (byte) 0x1F;

                            mBuffer[4] = 0x70;
                            mBuffer[5] = 0x03;
                            writeToCom(mBuffer);

                            break;
                        default:
                            break;
                    }
                default:
                    break;
            }

        }

        private void sleep(int ms) {
            try {
                Thread.sleep(ms);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }

        //// 回应模块基础信标变化,模块停止重发,否则模块会不断发送
        private void responseBaseStatusChange(byte[] data, int size) {
            byte[] retData = Arrays.copyOf(data, size);
            retData[4] = (byte) 0xF1;
            writeToCom(retData);
        }



        // 回应模块投票信标变化,模块停止重发,否则模块会不断发送
        private void responseVoteStatusChange(byte[] data, int size) {
            final byte[] retData = Arrays.copyOf(data, size);
            retData[4] = (byte) 0xF2;
            writeToCom(retData);

        }

        // 回应发送成功通知
        private void responseVoteSendSuccess(byte[] data, int size) {

            byte[] retData = Arrays.copyOf(data, size);
            retData[4] = (byte) 0xF3;
            writeToCom(retData);

            int serialNo = data[5] & 0xff;
            VoteResultItem ret = removeSentItem(serialNo);

            if (serialNo == allOkSerialNumber) {
                m_listener.onVoteSubmitAllOkSuccess();
                allOkSerialNumber = -1;
            } else if (serialNo == serviceSerialNumber) {
                m_listener.onServiceSubmitSuccess();
                serviceSerialNumber = -1;
            } else {
                if (ret != null) {
                    m_listener.onVoteSubmitSuccess(ret);
                }
            }
        }

        //回复清空键盘数据
        private void responseClearKeyboardData(byte[] data, int len) {
            byte[] retData = Arrays.copyOf(data, len);
            retData[4] = (byte) 0xB0;
//            retData[8] = 1;  修改原因:肖翔给的表达:人大的表决就是1,评分的是0
            writeToCom(retData);
        }

        //回复键盘授权数据
        private void responseAuthrizeKeyboardData(byte[] data, int len) {
            byte[] retData = Arrays.copyOf(data, len);
            retData[4] = (byte) 0xB0;
            writeToCom(retData);
        }


    }

    // 处理基础信标变化
    private void onBaseInfo(byte[] data) {

        // Arrays.fill(broadcastData, (byte) 0x0);//清空多包结果
        if(data != null){
            baseSign = new byte[data.length];
            System.arraycopy(data,0,baseSign,0,data.length);
        }

        BaseInfo info = new BaseInfo();
        info.baseId = data[5] & 0xff;// baseID
        info.idMode = data[6] & 0xff;
        info.confId = ((data[7] & 0xff) << 8) | (data[8] & 0xff);

        info.billId = data[9] & 0xff;
        info.authCode = (data[10] & 0xff) << 8 | (data[11] & 0xff);
        info.login = data[12] & 0xff;
        info.report = data[13] & 0xff;
        info.offTime = data[14] & 0xff;
        info.attrib = data[15] & 0xff;
        info.pageNo = (data[16] & 0xff) << 8 | (data[17] & 0xff);
        byte[] bname = Arrays.copyOfRange(data, 16, 16 + 12);
        info.baseName = new String(bname);

        m_listener.onBaseEvent(info);
    }

    // 处理投票信标变化
    private void onVoteInfo(byte[] data) {
        if(data != null){
            voteSign = new byte[data.length];
            System.arraycopy(data,0,voteSign,0,data.length);
        }
        byte[] dt = Arrays.copyOfRange(data, 4, data.length - 4 - 1);// 从len开始,和文档下标统一
        VoteInfo info = new VoteInfo();
        info.baseId = dt[1] & 0xff;
        info.nowT = dt[2] & 0xff << 8 | dt[3] & 0xff;
        info.dataPos = dt[4] & 0xff;
        info.mode = dt[5] & 0xff;
        info.mode1_msgType = dt[6] & 0xff;
//        if (info.mode != VoteType_Stop) {
//            serialNumber = 0;
//        }

        if (info.mode == VoteType_Stop) { // 停止
            if (info.mode1_msgType == 2) { // 有结果

                info.resultInfo.resultType = dt[7] & 0xff;
                info.resultInfo.bits = dt[8] & 0xff;
                info.resultInfo.num0 = (dt[9] & 0xff) << 8 | (dt[10] & 0xff);
                info.resultInfo.num1 = (dt[11] & 0xff) << 8 | (dt[12] & 0xff);
                info.resultInfo.num2 = (dt[13] & 0xff) << 8 | (dt[14] & 0xff);
                info.resultInfo.num3 = (dt[15] & 0xff) << 8 | (dt[16] & 0xff);
                info.resultInfo.num4 = (dt[17] & 0xff) << 8 | (dt[18] & 0xff);
                info.resultInfo.num5 = (dt[19] & 0xff) << 8 | (dt[20] & 0xff);
                info.resultInfo.num6 = (dt[21] & 0xff) << 8 | (dt[22] & 0xff);
            }
        } else if (info.mode == VoteType_BatchVote) {
            printDataBuf(data, data.length, "batchvote:");
            info.less = dt[11] & 0xff;
            info.mode3_secret = dt[12] & 0xff;
            info.fixballot = dt[13] & 0xff ;
            if(info.fixballot > 0 ){
                info.limitFavor = dt[14] & 0xff ;
                info.limitOppo = dt[15] & 0xff ;
                info.limitWaiver = dt[16] & 0xff ;
            }else{
                info.limitFavor = 0 ;
                info.limitOppo = 0 ;
                info.limitWaiver = 0;
            }


            info.mode2_modify = dt[23] & 0xff;
            info.voteid = dt[21] & 0xff;
        } else if (info.mode == VoteType_Choice) {
            info.mode2_modify = dt[7] & 0xff;
            info.mode3_secret = dt[8] & 0xff;
            info.mode4 = dt[9] & 0xff;
            info.mode5 = dt[10] & 0xff;
            info.mode6 = dt[11] & 0xff;
            info.mode7 = dt[12] & 0xff;
        } else if (info.mode == VoteType_BatchElect) {//选举
            info.electInfo.type = dt[6] & 0xff;
            info.electInfo.random = dt[7] & 0xff;
            info.electInfo.select = dt[8] & 0xff;
            info.electInfo.other = dt[9] & 0xff;
            info.electInfo.less = dt[10] & 0xff;
            info.electInfo.secrecy = dt[11] & 0xff;
            info.electInfo.modify = dt[12] & 0xff;
            info.electInfo.start = dt[13] & 0xff;
            info.electInfo.end = dt[14] & 0xff;
            info.electInfo.equityMode = dt[15] & 0xff;
            info.electInfo.minSelect = dt[16] & 0xff;
            info.electInfo.voteid = dt[21] & 0xff;

        } else if (info.mode == VoteType_SingleItemEvalue) {//单项评分
            info.singleEvalueInfo.mode1 = dt[6] & 0xff;
            info.singleEvalueInfo.mode2 = dt[7] & 0xff;
            info.singleEvalueInfo.mode3 = dt[8] & 0xff;
            info.singleEvalueInfo.mode4 = dt[9] & 0xff;
            info.singleEvalueInfo.mode5 = dt[10] & 0xff;
            info.singleEvalueInfo.mode6 = dt[11] & 0xff;
            info.singleEvalueInfo.mode7 = dt[12] & 0xff;
            info.singleEvalueInfo.mode8 = dt[13] & 0xff;
            info.singleEvalueInfo.mode9 = dt[14] & 0xff;
            info.singleEvalueInfo.mode10 = dt[15] & 0xff;
            info.singleEvalueInfo.mode11 = dt[16] & 0xff;
            info.singleEvalueInfo.mode12 = dt[17] & 0xff;
            info.singleEvalueInfo.title = new byte[12];

            System.arraycopy(dt, 12, info.singleEvalueInfo.title, 0, 12);
            info.voteid = dt[23] & 0xFF ;
            info.singleEvalueInfo.number = dt[24] & 0xff;
        } else if (info.mode == VoteType_BatchEvalue) {
            info.batchEvalueInfo.mode1 = dt[6] & 0xff;
            info.batchEvalueInfo.startVal = (dt[7] & 0xff) << 8 | (dt[8] & 0xff);
            info.batchEvalueInfo.endVal = (dt[9] & 0xff) << 8 | (dt[10] & 0xff);
            info.batchEvalueInfo.autoMove = dt[11] & 0xff;
            info.batchEvalueInfo.width1 = dt[12] & 0xff;
            info.batchEvalueInfo.width2 = dt[13] & 0xff;
            info.batchEvalueInfo.less = dt[14] & 0xff;
            info.batchEvalueInfo.secrecy = dt[15] & 0xff;
            info.batchEvalueInfo.modify = dt[16] & 0xff;
            info.batchEvalueInfo.ruleStart = dt[17] & 0xff;
            info.batchEvalueInfo.ruleEnd = dt[18] & 0xff;
            info.batchEvalueInfo.sliderover = dt[19] & 0xff;
            info.batchEvalueInfo.billNo = dt[20] & 0xff;
            info.batchEvalueInfo.billSubNo = ((dt[21] & 0xff) << 8) | (dt[22] & 0xff);

        } else if (info.mode == VoteType_BatchIDEvalue) {
            info.batchEvalueInfo.mode1 = dt[6] & 0xff;
            info.batchEvalueInfo.startVal = (dt[7] & 0xff) << 8 | (dt[8] & 0xff);
            info.batchEvalueInfo.endVal = (dt[9] & 0xff) << 8 | (dt[10] & 0xff);
            info.batchEvalueInfo.autoMove = dt[11] & 0xff;
            info.batchEvalueInfo.width1 = dt[12] & 0xff;
            info.batchEvalueInfo.width2 = dt[13] & 0xff;
            info.batchEvalueInfo.less = dt[14] & 0xff;
            info.batchEvalueInfo.secrecy = dt[15] & 0xff;
            info.batchEvalueInfo.modify = dt[16] & 0xff;
            info.batchEvalueInfo.ruleStart = dt[17] & 0xff;
            info.batchEvalueInfo.ruleEnd = dt[18] & 0xff;
            info.batchEvalueInfo.sliderover = dt[19] & 0xff;
            info.batchEvalueInfo.billNo = dt[20] & 0xff;
            info.batchEvalueInfo.billSubNo = ((dt[21] & 0xff) << 8) | (dt[22] & 0xff);
        } else {
            info.mode2_modify = dt[7] & 0xff;
            info.mode3_secret = dt[8] & 0xff;
            info.mode4 = dt[9] & 0xff;
            info.voteid = dt[11] & 0xff;
            info.file = dt[12] & 0xff;
            info.init = dt[13] & 0xff;
        }
        m_listener.onVoteEvent(info);
    }

    public void cleanSendQueue() {
        LogUtil.i(TAG,"cleanSendQueue");
        synchronized (sendQueen){
            LogUtil.i(TAG,"投票信标变化,清除已发队列");
            for(VoteResultItem temp : sendQueen){
                LogUtil.i(TAG,"清除数据",temp.data);
            }
            sendQueen.clear();
        }
    }

    private byte[] broadcastData = new byte[4096];
    private byte[] fileData;
    private int fileLen;
    private int dataLen;
    private int lastPackH = -1;

    private short needBits = 0;
    private int okBits = 0;
    private int currDownloadMsgId;
    private int broadMsgId = -1;
    private long lastOnMultiPackageDataTime;

    private int maxPackH; //用于计算收到长度
    private int maxPachL; //用于计算收到长度

    private int downType;
    private int downId;

    private boolean isMineDonwload = false ;

    /**
     * 字节	标识符	描述
     * 1	DOWNCMD	0x40 下载多包类指令
     * 2-3	KEYID	表决器编号,2字节,高位在前
     * 一般要指定表决器编号使指定键盘进入或退出下载状态
     * 但0x0000广播时候,键盘也执行
     * FF01到FFFE时候,是用序列号指定键盘 V4.74
     * 低位字节01-FE,表示下载次序,由SDK管理,新下载变化一次,
     * 键盘收到的时候,记录下,同时,此次序也出现在下载数据包中,这样键盘可判断是否是我可以下载的数据,避免上次下载状态没正常退出而错误接受数据
     * 4	DOWNCMD	1 进入或退出下载状态
     * 5	DOWNTYPE	多包类型
     * 6	DOWNID	数据包标识码
     * 7	DCMD	模式 1进入下载 0退出下载
     * 8-24	FILENAME	在DOWNTYPE=40下载文件模式时候,是下载文件名称,16字符
     * 其他模式参数无意义
     * SN	序列号模式时候,6字节键盘序列号,用于指定键盘
     * 暂不支持文件下载
     *
     * @param data
     * @param size
     */
    private void onMultiPackageInfo(byte[] data, int size) {
        byte[] dt = Arrays.copyOfRange(data, 3, data.length);// 从len开始,和文档下标统一
        int downCmd = dt[4]; //1 进入或退出下载状态

        int dcmd = dt[7];// 1:进入下载 0:退出下载

        if (downCmd == 1) { // 1:进入/退出下载
            resopnseMultiPackageMode(data, size);
            if (dcmd == 1) { //模式 1进入下载
                int keyId = (data[5] << 8) | data[6];
                if (keyId == onLineInfo.keyId || keyId == 0 || keyId == 0xFFFF) {
//                    isMineDonwload = true;
                    downType = dt[5];// 10即时信息 或 11短信 40 下载文件
                    downId = dt[6]; ////数据包标识码
                    LogUtil.d(TAG, " 1:进入下载    。。。。。downType = " + downType + " downId:" + downId);
                    lastPackH = -1;
                    if (downType == 40) { //下载文件模式  @Auther Elvis
                        int packH = (dt[8] & 0xff);
                        int packL = (dt[9] & 0xff);
                        fileLen = packH * 256 + packL + 1;
                        int len = 0;
                        for (int i = 10; i < 26; i++) {
                            if (dt[i] == 0) {
                                break;
                            }
                            len++;
                        }
                        String fileName = "test.txt";
                        try {
                            fileName = new String(dt, 10, len, "GB2312");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        dataLen = fileLen + 36;
                        LogUtil.d(TAG, "下载文件,长度:" + fileLen + ",  文件名:" + fileName);
                        if (fileLen > 0) {
                            fileData = new byte[dataLen];
                            Arrays.fill(fileData, (byte) 0x0);
                        }

                        try {
                            RFFileDownloadModule.getInstance().getDownloadProcess().setFileName(fileName);
                            RFFileDownloadModule.getInstance().getDownloadProcess().setLength(fileLen - 4);
                            RFFileDownloadModule.getInstance().getDownloadProcess().onStartDownload();
                        } catch (Exception e1) {
                        }


                    } else if (downType >= 1 && downType <= 3) {
                        m_listener.onMultiPackageStartDownload(downType, downId);
                        maxPackH = 0;
                        maxPachL = 0;
                        Arrays.fill(broadcastData, (byte) 0x0);
                    } else {
                        maxPackH = 0;
                        maxPachL = 0;
                        Arrays.fill(broadcastData, (byte) 0x0);
                    }
                }
            } else { //0退出下载
                int keyId = (data[5] << 8) | data[6];
                if (keyId == onLineInfo.keyId || keyId == 0 || keyId == 0xFFFF) {
                    isMineDonwload = false;
                    LogUtil.d(TAG, dcmd + ": 退出下载。。。。。。。。。");
                    if (downType == 40) {
                        //@Auther Elvis 添加文件下载逻辑
                        if (System.currentTimeMillis() - lastOnMultiPackageDataTime > 500) {
                            lastOnMultiPackageDataTime = System.currentTimeMillis();
                            try {
                                if (fileData != null) {
                                    File file = RFFileDownloadModule.getInstance().getDownloadProcess().getFile();
                                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                                    fileOutputStream.write(fileData, 0, fileLen - 4);
                                    fileOutputStream.flush();
                                    fileOutputStream.close();
                                    fileData = null;
                                    RFFileDownloadModule.getInstance().getDownloadProcess().onStopDownload();
                                }
                            } catch (Exception ex) {
                                LogUtil.e(TAG, ex);
                                RFFileDownloadModule.getInstance().getDownloadProcess().onDownloadError(ex);
                            }
                        }

                    } else if (downType >= 1 && downType <= 3) {
                        if (System.currentTimeMillis() - lastOnMultiPackageDataTime > 500) {
                            lastOnMultiPackageDataTime = System.currentTimeMillis();
                            LogUtil.d(TAG, "maxPackH:" + maxPackH + "   maxPachL:" + maxPachL);
                            broadcastData[0] = (byte) 0xF4;
                            broadcastData[1] = (byte) 0xF5;
                            broadcastData[2] = (byte) downType;
                            broadcastData[3] = (byte) downId;
                            m_listener.onMultiPackageData(broadcastData, (maxPackH + 1) * 16 + (maxPachL + 1) * 16 + 4);
                        }
                    }
                }
            }
        } else if (downCmd == 2) { // 2:下载数据
//            if(isMineDonwload) {
                int msgid = (byte) dt[6] & 0xFF;
                byte packH = dt[7];// 数据段 编号 当前最大15(16片)
                byte packL = dt[8];// 数据片编号
                LogUtil.d(TAG, "下载。。。msgId:" + msgid + "  packH:" + packH + " apckL:" + packL);

                if (downType == 40) {// 文件下载
                    if (lastPackH != packH) { //新的packH
                        LogUtil.i(TAG, "new packH.......");
                        okBits = 0;
                        lastPackH = packH;
                    }
                    int destPos = ((packH & 0xFF) * 16 + (packL & 0xFF)) * 16;
                    if (destPos + 16 <= fileData.length) {
                        System.arraycopy(dt, 9, fileData, destPos, 16);
                    }

                    if ((okBits & (1 << packL)) == 0) {// 添加文件下载逻辑 进度
                        RFFileDownloadModule.getInstance().getDownloadProcess().onDownloadAddProcess(16);
                    }
                    okBits |= (1 << packL);
                    LogUtil.d(TAG, "packL:" + packL + "   okBits:" + okBits);

                } else {// 广播


                    if (broadMsgId != msgid) {
                        Arrays.fill(broadcastData, (byte) 0x0);
                        broadMsgId = msgid;
                        okBits = 0;
                    }
                    if (lastPackH != packH) {
                        LogUtil.i(TAG, "new packH.......");
                        needBits = calcBits(packH);
                        okBits = 0;
                        lastPackH = packH;
                        maxPachL = 0;
                    }

                    if (packH > maxPackH) {
                        maxPackH = packH;
                    }
                    if (packL > maxPachL) {
                        maxPachL = packL;
                    }

                    if (downType >= 1 && downType <= 3) {
                        System.arraycopy(dt, 9, broadcastData, (packH * 16 + packL) * 16 + 4, 16);
                    } else {
                        System.arraycopy(dt, 9, broadcastData, packL * 16, 16);//短信 packH  总共多少片, packL 第几片
                    }
                    okBits |= (1 << packL);
                    LogUtil.d(TAG, "packL:" + packL + "   okBits:" + okBits + "  needBits:" + (needBits & 0xffff) + " downType:" + downType);
                    if (downType == 0 || downType > 3) {

                        if (okBits == needBits) {
                            if (broadMsgId != currDownloadMsgId) {
                                m_listener.onMultiPackageData(broadcastData, (packH + 1) * 16);
                                currDownloadMsgId = broadMsgId;
                            }
                            Arrays.fill(broadcastData, (byte) 0x0);
                            broadMsgId = -1;
                            lastPackH = -1;
                            //okBits = 0;

                        }
                    }
                }
//            }
        } else if (downCmd == 3) { // 3:询问
            resopnseMultiPackageQuery(data, size);
            LogUtil.d(TAG, " 3:询问。。。。。。。。。");


        }
    }

    private short calcBits(byte packh) {
        short bits = 0;
        for (int i = 0; i <= packh; i++) {
            bits |= (1 << i);
        }
        return bits;
    }

    private void resopnseMultiPackageMode(byte[] data, int size) {
        byte[] retData = Arrays.copyOf(data, size);
        retData[4] = (byte) 0xC0;
        writeToCom(retData);
    }

    private void resopnseMultiPackageQuery(byte[] data, int size) {
        byte[] retData = Arrays.copyOf(data, size);
        retData[4] = (byte) 0xC0;
        int tmpOk = ~okBits;
        retData[11] = (byte) tmpOk;
        retData[12] = (byte) (tmpOk >> 8);
        writeToCom(retData);
    }

    public static String getVoteType(int mode) {
        switch (mode) {
            case VoteType_Stop:
                return "VoteType_Stop";
            case VoteType_Signin:
                return "VoteType_Signin";
            case VoteType_Vote:
                return "VoteType_Vote";
            case VoteType_BatchVote:
                return "VoteType_BatchVote";
            case VoteType_BatchElect:
                return "VoteType_BatchElect";

            default:
                break;
        }
        return "" + mode;
    }

    public static String getKeySn(byte[] data) {
        String sn = "";
        String CS = "0123456789ABCDEF";
        for (int i = 0; i < 6; i++) {
            sn += CS.charAt((data[i] >> 4) & 0xF);
            sn += CS.charAt((data[i] >> 0) & 0xF);
        }
        return sn;

    }

    private long writeOkPageNo;
    private int compareCount;

    private class CheckFirmReceveThread extends Thread {
        public void run() {
            while (true) {
                try {
                    Thread.sleep(2000);
                    if (writeOkPageNo != firmWritePage) {
                        writeOkPageNo = firmWritePage;
                        //sendMessage(keypadID + ":recv"+lastReceveLength+"\n");
                        //LogUtil.d(TAG,"CheckReceveThread:check ok!");
                        compareCount = 0;
                    } else {
                        compareCount++;
                        Thread.sleep(2000);
                        if (compareCount <= 20) {
                            LogUtil.d(TAG, "CheckWriteFirmhread:retry ===========");

                            m_listener.onFirmUpdateInfo("与入页:" + firmWritePage + "失败, 次数:" + compareCount);
                            writeFlash(firmWritePage);
                        } else {
                            compareCount = 0;

                            LogUtil.d(TAG, "CheckWriteFirmhread:fail , exit!");

                            LogUtil.e(TAG, "写入扇区" + firmWritePage + "失败!");
                            m_listener.onFirmUpdateResult(false, "写入扇区" + firmWritePage + "失败!");
                            exitFirmUpdate(2);

                            break;
                        }
                    }


                    if (firmWritePage > 0 && firmWritePage == firmWritePageMax) {
                        LogUtil.d(TAG, "CheckWriteFirmhread:ok : exit!");
                        break;
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    ;

    private static int encodeBCD(byte c) {
        if (c >= 'a' && c <= 'f') {
            return 10 + (c - 'a');
        } else if (c >= '0' && c <= '9') {
            return c - '0';
        } else if (c >= 'A' && c <= 'F') {
            return 10 + (c - 'A');
        } else if (c == '.') {
            return 0xE;
        }
        return -1;
    }


    /**
     * 把字符串形式转换为字节形式
     */
    private byte[] util_encodeBCD(byte[] cs) {
        byte[] rs = new byte[17];
        Arrays.fill(rs, (byte) 0xff);//

        int len = cs.length < 16 ? cs.length : 16;
        int io = 0;

        for (int i = 0; i < len; i += 2) {
            int n = encodeBCD(cs[i]);
            n <<= 4;
            if (i + 1 == cs.length) {
                rs[io++] = (byte) (n | 0xf);
                break;
            }

            n |= encodeBCD(cs[i + 1]);
            if (n < 0) {
                break;
            }
            rs[io++] = (byte) n;
        }
        rs[++io] = 0;
        return rs;
    }

    public static String util_decodeBCD(byte[] cs, int len) {
        byte[] buf = new byte[8 * 2];
        Arrays.fill(buf, (byte) 0);
        String bcdNum = "0123456789ABCD.F";
        String bcdStr = bytesToHexString(cs);
        for (int i = 0; i < len * 2; i += 2) {
            byte item = bcdStr.getBytes()[i];
            if (item >= '0' && item <= '9') {
                buf[i] = item;
            } else if (item == 'E') {
                buf[i] = '.';
            } else if (item == 'F') {
                buf[i] = 0;
                break;
            }

            item = bcdStr.getBytes()[i + 1];
            if (item >= '0' && item <= '9') {
                buf[i + 1] = item;
            } else if (item == 'E') {
                buf[i + 1] = '.';
            } else if (item == 'F') {
                buf[i + 1] = 0;
                break;
            }

        }
        return new String(buf).trim();

    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString().toUpperCase();
    }


    /**
     * @param length   长度
     * @param filename 文件名
     * @Auther Elvis
     * 主动发起文件申请
     */
    public void applyFileUpload(int length, String filename, byte anstype,byte index) {
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = 0x73;
        mBuffer[5] = (byte) 0XFF;// 流水号
        mBuffer[6] = (byte) 0X01;// 流水号

        mBuffer[7] = (byte) 15;// MSGTYPE

        mBuffer[8] = (byte) anstype;// ANSTYPE   添加5 文件上传指令
        mBuffer[9] = index;// 高位
//        mBuffer[10] = (byte) length;// 低位

//        byte[] bytes = new byte[16];
//        try {
//            if (filename != null) {
//                bytes = filename.getBytes("GB2312");
//            }
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//
//        if (bytes != null) {
//            for (int i = 0; i < 16 && i < bytes.length; i++) {
//                mBuffer[11 + i] = bytes[i];
//            }
//        }

        writeToCom(mBuffer);
    }

    /**
     * @param keyid  键盘id
     * @param packid 包id
     * @param packH  段id
     * @param packL  片id
     * @param datas  数据
     * @Auther Elvis
     */
    public void uploadFileData(byte[] keyid, byte packid, byte packH, byte packL, byte[] datas, byte packtype) {
        uploadFileData(keyid, packid, packH, packL, datas, 0, datas.length, packtype);
    }

    /**
     * @param keyid  键盘编号
     * @param packid 包id
     * @param packH  数据段id
     * @param packL  数据片id
     * @param datas  数据
     * @param offset 偏移位置
     * @param length 长度
     * @Auther Elvis
     * 上传数据包(多包应答)
     */
    public void uploadFileData(byte[] keyid, byte packid, byte packH, byte packL, byte[] datas, int offset, int length, byte packtype) {
        LogUtil.i(TAG, "onMutilPacketData packid=" + packid + ",packH=" + packH + ",packL=" + packL + ",offset=" + offset);
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = (byte) 0xA0;
        if(keyid == null || keyid.length < 2){
            if(onLineInfo != null){
                keyid[0] = (byte)((onLineInfo.keyId >> 8 ) & 0xFF) ;
                keyid[1] = (byte)((onLineInfo.keyId ) & 0xFF) ;
            }else{
                keyid = new byte[2];
            }
        }
        mBuffer[5] = keyid[0];
        mBuffer[6] = keyid[1];

        mBuffer[7] = 2;//CMD	1 回应多包数据
        mBuffer[8] = packtype;//PACKTYPE	多包类型
        mBuffer[9] = packid;//数据包标识码
        mBuffer[10] = packH;//数据段编号
        mBuffer[11] = packL;//数据片编号
        for (int i = 0; i < length && i < 16 && i < datas.length - offset; i++) {
            mBuffer[12 + i] = datas[offset + i];
        }
        writeToCom(mBuffer);
    }

    /**
     * @param packid 包id
     * @Auther Elvis
     * 多包接收确认(多包接收应答)
     */
    public void packetConfirmation(byte[] keyid, byte packid, byte packH, byte packL, byte[] names, byte packtype) {
        LogUtil.i(TAG, "onMutilPacketRespone packid=" + packid + ",packH=" + packH + ",packL=" + packL + ",names=" + names);
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = (byte) 0xA0;
        if(keyid == null || keyid.length < 2){
            if(onLineInfo != null){
                keyid[0] = (byte)((onLineInfo.keyId >> 8 ) & 0xFF) ;
                keyid[1] = (byte)((onLineInfo.keyId ) & 0xFF) ;
            }else{
                keyid = new byte[2];
            }
        }
        mBuffer[5] = keyid[0];
        mBuffer[6] = keyid[1];

        mBuffer[7] = 1;//CMD	1
        mBuffer[8] = packtype;//PACKTYPE	多包类型
        mBuffer[9] = packid;//数据包标识码
        mBuffer[10] = packH;
        mBuffer[11] = packL;
        if (names != null) {
            for (int i = 0; i < 14 && i < names.length; i++) {
                mBuffer[12 + i] = names[i];
            }
        }
        writeToCom(mBuffer);
    }

    /**
     * @param packid 包id
     * @param packH  数据段id
     * @Auther Elvis
     * 多包接收完毕确认(多包接收完毕确认应答)
     */
    public void packetReceptionConfirmed(byte[] keyid, byte packid, byte packH, byte packtype) {
        LogUtil.i(TAG, "onMutilPacketRespone packid=" + packid + ",packH=" + packH);
        byte[] mBuffer = new byte[0x1F + 4];
        Arrays.fill(mBuffer, (byte) 0x0);
        mBuffer[0] = (byte) 0xF5;
        mBuffer[1] = (byte) 0xAA;
        mBuffer[2] = (byte) 0xAA;
        mBuffer[3] = (byte) 0x1F;

        mBuffer[4] = (byte) 0xA0;
        if(keyid == null || keyid.length < 2){
            if(onLineInfo != null){
                keyid[0] = (byte)((onLineInfo.keyId >> 8 ) & 0xFF) ;
                keyid[1] = (byte)((onLineInfo.keyId ) & 0xFF) ;
            }else{
                keyid = new byte[2];
            }
        }
        mBuffer[5] = keyid[0];
        mBuffer[6] = keyid[1];

        mBuffer[7] = 3;//CMD	3 多包接收完毕应答
        mBuffer[8] = packtype;//PACKTYPE	多包类型
        mBuffer[9] = packid;//数据包标识码
        writeToCom(mBuffer);
    }

    /**
     * @param datas  基站传递过来的数据
     * @param length 长度
     * @Auther Elvis
     */
    public void onMutilPacketRequest(byte[] datas, int length) {
        LogUtil.i(TAG ,"onMutilPacketRequest",datas);
        byte[] keyid = new byte[2];
        keyid[0] = datas[5];
        keyid[1] = datas[6];
        byte packid = datas[9];
        byte packH = datas[10];
        byte[] packLs = new byte[2];
        packLs[0] = datas[11];
        packLs[1] = datas[12];

        int keyId = (keyid[0] << 8) | keyid[1];
        if (keyId == onLineInfo.keyId) {
            if (datas[8] == 5) {
                RFFileUploadModule.getInstance().onUploadFile(datas[7], keyid, packid, packH, packLs);
            }
            if (datas[8] == 3) {
                RFMessageUploadModule.getInstance().onUploadMessage(datas[7], keyid, packid, packH, packLs);
            }
        }
    }

    public void openSendData(byte[] datas){
        writeToCom(datas);
    }



}