1/*
2 * Copyright (c) 2022-2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const { NapiLog } = require('./../hcs/NapiLog');
17let mockTest = [
18  {
19    fn: 'D:\\ceshi\\d.hcs',
20    data: `
21	#include "D:\\ceshi\\b.hcs"
22root {
23    nodeaa {
24		childnodechildnodechildnodechildnodechildnodechildnode {
25		}
26		childatt = "HDF_PLATFORMHDF_PLATFORMHDF_PLATFORMHDF_PLATFORM";
27    }
28	nodebb {
29	}
30	extint = 0;
31	extstring = "gpio_adapter";
32 }`,
33  },
34  {
35    fn: 'D:\\ceshi\\b.hcs',
36    data: `
37root {
38	 nodeaa {
39    }
40	nodebb {
41	}
42 }`,
43  },
44];
45
46function getArray(fn) {
47  for (let i in mockTest) {
48    if (mockTest[i].fn === fn) {
49      let ret = [];
50      for (let j in mockTest[i].data) {
51        ret.push(mockTest[i].data.charCodeAt(j));
52      }
53      return ret;
54    }
55  }
56  return null;
57}
58
59class MockMessage {
60  constructor() {}
61  send(type, data) {
62    setTimeout(() => {
63      const { XMessage } = require('./XMessage');
64      XMessage.gi().onRecv({
65        data: {
66          type: type,
67          data: data,
68        },
69      });
70    }, 100);
71  }
72  processMessage(msg) {
73    NapiLog.logInfo('---MockMessage start---');
74    NapiLog.logInfo(msg.type);
75    NapiLog.logInfo(msg.data);
76    if (msg.type === 'inited') {
77      this.send('parse', mockTest[0].fn);
78    } else if (msg.type === 'getfiledata') {
79      this.send('filedata', {
80        fn: msg.data,
81        data: getArray(msg.data),
82      });
83    }
84    NapiLog.logInfo('---MockMessage end---');
85  }
86}
87
88MockMessage.pInstance_ = null;
89MockMessage.gi = function () {
90  if (MockMessage.pInstance_ === null) {
91    MockMessage.pInstance_ = new MockMessage();
92  }
93  return MockMessage.pInstance_;
94};
95
96module.exports = {
97  MockMessage,
98};
99