aranea-sdk-cli 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/simulate.js +70 -7
- package/package.json +5 -4
|
@@ -48,9 +48,74 @@ const commander_1 = require("commander");
|
|
|
48
48
|
const chalk_1 = __importDefault(require("chalk"));
|
|
49
49
|
const ora_1 = __importDefault(require("ora"));
|
|
50
50
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
51
|
+
const axios_1 = __importDefault(require("axios"));
|
|
51
52
|
const fs = __importStar(require("fs"));
|
|
52
53
|
const path = __importStar(require("path"));
|
|
53
54
|
const config_1 = require("../config");
|
|
55
|
+
// Schema API
|
|
56
|
+
const SCHEMA_API_BASE = 'https://asia-northeast1-mobesorder.cloudfunctions.net/araneaSchemaAPI';
|
|
57
|
+
/**
|
|
58
|
+
* Fetch schema from API and generate sample state
|
|
59
|
+
*/
|
|
60
|
+
async function generateSampleStateFromSchema(type) {
|
|
61
|
+
try {
|
|
62
|
+
const response = await axios_1.default.get(`${SCHEMA_API_BASE}?action=get&type=${encodeURIComponent(type)}`);
|
|
63
|
+
if (!response.data.ok) {
|
|
64
|
+
console.warn(chalk_1.default.yellow(`スキーマ "${type}" が見つかりません。デフォルト値を使用します。`));
|
|
65
|
+
return { simulatedAt: new Date().toISOString() };
|
|
66
|
+
}
|
|
67
|
+
const schema = response.data.schema;
|
|
68
|
+
const stateProps = schema.stateSchema?.properties || {};
|
|
69
|
+
const sampleState = {};
|
|
70
|
+
// Generate sample values based on schema
|
|
71
|
+
for (const [name, def] of Object.entries(stateProps)) {
|
|
72
|
+
const fieldType = def.type;
|
|
73
|
+
if (fieldType === 'boolean') {
|
|
74
|
+
sampleState[name] = true;
|
|
75
|
+
}
|
|
76
|
+
else if (fieldType === 'number' || fieldType === 'integer') {
|
|
77
|
+
if (name.toLowerCase().includes('rssi')) {
|
|
78
|
+
sampleState[name] = -65;
|
|
79
|
+
}
|
|
80
|
+
else if (def.minimum !== undefined) {
|
|
81
|
+
sampleState[name] = def.minimum;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
sampleState[name] = 0;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else if (fieldType === 'string') {
|
|
88
|
+
sampleState[name] = def.format === 'date-time' ? new Date().toISOString() : `sample_${name}`;
|
|
89
|
+
}
|
|
90
|
+
else if (fieldType === 'object') {
|
|
91
|
+
// For nested objects like output1, output2, input1, input2
|
|
92
|
+
const nestedProps = def.properties || {};
|
|
93
|
+
const nestedSample = {};
|
|
94
|
+
for (const [nestedName, nestedDef] of Object.entries(nestedProps)) {
|
|
95
|
+
if (nestedDef.type === 'boolean') {
|
|
96
|
+
nestedSample[nestedName] = name.includes('output') ? false : true;
|
|
97
|
+
}
|
|
98
|
+
else if (nestedDef.type === 'number' || nestedDef.type === 'integer') {
|
|
99
|
+
nestedSample[nestedName] = 0;
|
|
100
|
+
}
|
|
101
|
+
else if (nestedDef.type === 'string') {
|
|
102
|
+
nestedSample[nestedName] = nestedDef.format === 'date-time' ? new Date().toISOString() : '';
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
sampleState[name] = nestedSample;
|
|
106
|
+
}
|
|
107
|
+
else if (fieldType === 'array') {
|
|
108
|
+
sampleState[name] = [];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
sampleState.simulatedAt = new Date().toISOString();
|
|
112
|
+
return sampleState;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.warn(chalk_1.default.yellow(`スキーマ取得エラー: ${error.message}。デフォルト値を使用します。`));
|
|
116
|
+
return { simulatedAt: new Date().toISOString() };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
54
119
|
exports.simulateCommand = new commander_1.Command('simulate')
|
|
55
120
|
.description('デバイス動作のシミュレーション');
|
|
56
121
|
// simulate state-report
|
|
@@ -79,6 +144,10 @@ exports.simulateCommand
|
|
|
79
144
|
}
|
|
80
145
|
else if (options.tid && options.lacisId && options.cic) {
|
|
81
146
|
// コマンドラインオプションから構築
|
|
147
|
+
// スキーマから動的にサンプル状態を生成
|
|
148
|
+
const spinner = (0, ora_1.default)(`スキーマ "${options.type}" を取得中...`).start();
|
|
149
|
+
const sampleState = await generateSampleStateFromSchema(options.type);
|
|
150
|
+
spinner.stop();
|
|
82
151
|
requestBody = {
|
|
83
152
|
auth: {
|
|
84
153
|
tid: options.tid,
|
|
@@ -87,13 +156,7 @@ exports.simulateCommand
|
|
|
87
156
|
},
|
|
88
157
|
report: {
|
|
89
158
|
type: options.type,
|
|
90
|
-
state:
|
|
91
|
-
// サンプル状態
|
|
92
|
-
Trigger1: true,
|
|
93
|
-
Trigger2: false,
|
|
94
|
-
RSSI: -65,
|
|
95
|
-
simulatedAt: new Date().toISOString(),
|
|
96
|
-
},
|
|
159
|
+
state: sampleState,
|
|
97
160
|
},
|
|
98
161
|
};
|
|
99
162
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aranea-sdk-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "AraneaSDK CLI - ESP32 IoTデバイス開発支援ツール",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -43,10 +43,11 @@
|
|
|
43
43
|
],
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"axios": "^1.6.0",
|
|
46
|
-
"commander": "^12.0.0",
|
|
47
46
|
"chalk": "^4.1.2",
|
|
48
|
-
"
|
|
49
|
-
"inquirer": "^8.2.6"
|
|
47
|
+
"commander": "^12.0.0",
|
|
48
|
+
"inquirer": "^8.2.6",
|
|
49
|
+
"node-fetch": "^2.7.0",
|
|
50
|
+
"ora": "^5.4.1"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
53
|
"@types/node": "^20.0.0",
|