# cdm-plugin
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [install：](#install)
- [use：](#use)
  - [Http：](#http)
  - [WebSocketClient：](#websocketclient)
  - [FileUtil：](#fileutil)
  - [BaseUtil：](#baseutil)
  - [BaseDBUtil：](#basedbutil)
    - [reference](#reference)
  - [Business：](#business)
  - [Log：](#log)
  - [DICT：](#dict)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

### install：
```
    npm install cdm-plugin --save
```

### use：
```
    import {Http, WebSocketClient, FileUtil, 
    BaseUtil, BaseDBUtil, Business, 
    LOGLEVEL, MODE_TYPE, FILE_TYPE, Log} from 'cdm-plugin';
```
#### Http：
##### method
| method | description | params | params detail |
| ------ | ------ | ------ | ------ |
| ajaxPromise | ajax with promise | {Object} |{url, data, async, type} default: type='get', async=true,data = '' |
| ajaxPromiseUpload | upload file | {Object} |{url, formData} |
##### use
```
    // post
    let data = await Http.ajaxPromise({url: `www.xxx.com/entity?id=1`}); // get
    let data = await Http.ajaxPromise({
        url: `www.xxx.com/entity`,
        type: 'post',
        data: data,
    });
```

```
    // upload file
    const export_blob = new Blob([data]);
    const file = new File([export_blob], fileName);
    const formData = new FormData();
    formData.append("file", file);
    Http.ajaxPromiseUpload({
        url,
        formData,
    });
```

#### WebSocketClient：
##### method
| method | description | params |
| ------ | ------ | ------ |
| constructor | init with ip, port | ip, port |
| addEventListener | - | type, callback |
| removeEventListener | - | type |
| removeAllEventListener | - | - |
| waitConnect | - | - |
| send | - | data |
| close | - | - |
##### use
```
    let client = new WebSocketClient(ip, port);
    client.addEventListener('devicelist', (e) => {
        console.log(e.data);
    });
    await client.waitConnect();
```

```
    client.removeEventListener('devicelist');
    client.removeAllEventListener();
```

```
    client.send(JSON.stringify(data));
    client.close();
```

#### FileUtil：
##### method
| method | description | params | params detail |
| ------ | ------ | ------ | ------ |
| XMLtoString | - | xmlElement | - |
| toFile | generate file | fileName, fileText | - |
| getZipFiles | get a zip package all files | file | - |
| saveZip | generate zip file | fileName, content | - |
| uploadFiles | - | url, files | - |
##### use
```
    const str = FileUtil.XMLtoString(xmlElement);
    cosnt file1 = FileUtil.toFile(str);
    cosnt file2 = FileUtil.toFile(str);
    const [logics, modes, zip] = await FileUtil.getZipFiles(file);
    const files = [file1, file2];
    FileUtil.uploadFiles(url, files);
```
```
    let zip = new JSZip();
    let logic = zip.folder('lua');
    cosnt file1 = FileUtil.toFile(str);
    logic.file(`${fileName}.${fileType}`, file1);
    const content = await zip.generateAsync({type: 'blob'});
    FileUtil.saveZip(`fileName.zip`, content);
```

#### BaseUtil：
##### method
| method | description | params | params detail |
| ------ | ------ | ------ | ------ |
| notEmptyArray | - | [Array] | return boolean |
| notEmptyObject | - | {Object} | return boolean |
| fileNameFormat | - | String | return String |
| fileExtFormat | - | String | return String |
##### use
```
    let array1;// false
    let array2 = [];// false
    let array3 = [0];// true
    BaseUtil.notEmptyArray(array1);
    
    let object1;// false
    let object2 = {};// false
    let object3 = {'key': 'value'};// true
    BaseUtil.notEmptyObject(object1);
```
```
    const fileName = 'index.json';
    BaseUtil.fileNameFormat(fileName);// index
    BaseUtil.fileExtFormat(fileName);// json
```

#### BaseDBUtil：
##### method
| method | description | params | params detail |
| ------ | ------ | ------ | ------ |
| setConfig | ------ | [Array] | return boolean |
| setTimeout | ------ | number | return boolean |
| getTimeout | ------ | - | return number |
| getInstance | ------ | - | return number |
| save2Idb | ------ | tableName, data | - |
| findDataByCondition | ------ | tableName, condition | return [Array] |
| deleteData | ------ | tableName, condition | - |
| customOperation | ------ | tableName, operation | callback return instance |

##### use
```
import dbc from '@/js/db_config';

    const baseDBUtil = new BaseDBUtil();
    baseDBUtil.setConfig(dbc);
    baseDBUtil.setTimeout(5000);
    await baseDBUtil.getInstance();
    await baseDBUtil.save2Idb(tableName,{'key':'value'});
    const data = await baseDBUtil.findDataByCondition(tableName, (item) => {
        return item.programName === name
            && item.programType === type;
    });
    const data2 = await baseDBUtil.findAll(tableName);
    await baseDBUtil.deleteData(tableName, (item) => {
        return item.name === key && item.type === FILE_TYPE.JSON;
    });
    await baseDBUtil.customOperation(tableName, (instance) => {
        instance.query({
            tableName,
            condition,
        });
    });
    await baseDBUtil.clearIdbTable(tableName);
```
##### reference
when you use customOperation, you can get idb-js instance

db_config file reference idb-js

idb-js doc url: https://github.com/verybigorange/idb-js

#### Business：
##### method
| method | description | params | params detail |
| ------ | ------ | ------ | ------ |
| getZipData | zip to data | File | return [Array] |
| generateZipContent | data to zip | data | return content |
| uploadAllFiles | - | config, logicFiles, modeFiles | config = {fileServer, params} |
| deleteAllFiles | - | config, logicFiles, modeFiles | config = {fileServer, params} |

##### use
```
    const rows = Business.getZipData(file);
    rows.forEach((item) => {
        baseDBUtil.save2Idb(tableName, item);
    });
    
    const content = Business.generateZipContent(rows);
    FileUtil.saveZip(fname, content);
```
```
    const config = {
        fileServer: 'www.xxx.com/entity', 
        params: '?id=1',
    };
    await Business.uploadAllFiles(config, logicFiles, modeFiles);
    await Business.deleteAllFiles(config, logicFiles, modeFiles);
```

#### Log：
##### method
| method | description | params | params detail |
| ------ | ------ | ------ | ------ |
| init | - | logLevel | number[0-4] |
| setLevel | - | logLevel | number[0-4] |
##### use
```
    Log.init(LOG_LEVEL.WARN);
    Log.setLevel(LOG_LEVEL.INFO);
```

#### DICT：
##### define
| object| key | value |
| ------ | ------ | ------ |
| LOG_LEVEL | TRACE | 0 |
| LOG_LEVEL | LOG | 1 |
| LOG_LEVEL | INFO | 2 |
| LOG_LEVEL | WARN | 3 |
| LOG_LEVEL | ERROR | 4 |
| MODE_TYPE | LOGIC | 'logic' |
| MODE_TYPE | MODE | 'mode' |
| FILE_TYPE | JSON | 'json' |
| FILE_TYPE | JSON | 'lua' |
| FILE_TYPE | JSON | 'xml' |
##### use
```
    Log.init(LOG_LEVEL.WARN);
```