
MongoDB を使用してシンプルなスマート ホーム システムを開発する方法
スマート ホーム システムは現代の家庭生活の一部になっています。スマートホームシステムの助けを借りて、携帯電話やその他のデバイスを介して、照明、電化製品、ドアロックなどの家のさまざまなデバイスをリモート制御できます。この記事では、MongoDB を使用してシンプルなスマート ホーム システムを開発する方法を紹介し、読者の参考となる具体的なコード例を示します。
1. システム要件の分析
開発を開始する前に、まずシステム要件を明確にする必要があります。シンプルなスマートホーム システムには次の機能が必要です。
- ユーザーのログインと登録: ユーザーはアカウントを登録し、ログインすることでシステムを使用できます。
- デバイス管理: ユーザーは、照明、電化製品、ドアロックなどのさまざまなデバイスを追加、削除、制御できます。
- スケジュールされたタスク: ユーザーは、照明や電化製品をスケジュールされた時間にオン/オフするなど、スケジュールされたタスクを設定できます。
- 履歴記録: システムは、ユーザーが閲覧できるように、ユーザーのデバイスの制御履歴を記録する必要があります。
2. データベース設計
上記の要件に基づいて、次のデータベース構造を設計できます:
-
ユーザー テーブル (ユーザー) :
- _id: ユーザー ID
- username: ユーザー名
- password: パスワード
-
デバイス テーブル ( devices):
- _id: デバイス ID
- name: デバイス名
- type: デバイスの種類
- status: デバイスのステータス (オン/オフ) )
- user_id: ユーザー ID
-
スケジュールされたタスク リスト (タスク):
- _id: タスク ID
- name: タスク名
- device_id: デバイス ID
- user_id: ユーザー ID
- time: タスクの実行時間
- #操作記録テーブル(レコード): #_id: レコードID
- device_id: デバイスID
- user_id: ユーザーID
- action:動作(on/off)
- time: 動作時間
-
3. システム開発
次にMongoDBとNodeを使います。 js を使用してスマート ホーム システムを開発します。
環境の準備
- まず、Node.js と MongoDB がインストールされていることを確認し、MongoDB サービスを開始します。
プロジェクトの作成と依存関係のインストール
- コマンド ラインで次のコマンドを実行して、新しい Node.js プロジェクトを作成し、対応する依存関係をインストールします:
1 2 3 4 | mkdir smart-home-system
cd smart-home-system
npm init -y
npm install express mongodb
|
ログイン後にコピー
データベース接続の作成
- #ルート ディレクトリに
db.js
ファイルを作成し、次の内容を追加します:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const { MongoClient } = require ( 'mongodb' );
async function connect() {
try {
const client = await MongoClient.connect( 'mongodb://localhost:27017' );
const db = client.db( 'smart-home-system' );
console.log( 'Connected to the database' );
return db;
} catch (error) {
console.log( 'Failed to connect to the database' );
throw error;
}
}
module.exports = { connect };
|
ログイン後にコピー
作成ルートとコントローラー
- ルート ディレクトリに
routes
フォルダーを作成し、次のルーティング ファイル
devices.js:
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 | const express = require ( 'express' );
const { ObjectId } = require ( 'mongodb' );
const { connect } = require ( '../db' );
const router = express.Router();
router.get( '/' , async (req, res) => {
try {
const db = await connect();
const devices = await db.collection( 'devices' ).find().toArray();
res.json(devices);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.post( '/' , async (req, res) => {
try {
const { name, type, status, user_id } = req.body;
const db = await connect();
const result = await db.collection( 'devices' ).insertOne({
name,
type,
status,
user_id: ObjectId(user_id),
});
res.json(result.ops[0]);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
module.exports = router;
|
ログイン後にコピー
Create を追加します。ルート ディレクトリに
controllers
フォルダーを作成し、次のコントローラー ファイル
devicesController.js:
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 | const { connect } = require ( '../db' );
async function getDevices() {
try {
const db = await connect();
const devices = await db.collection( 'devices' ).find().toArray();
return devices;
} catch (error) {
throw error;
}
}
async function createDevice(device) {
try {
const db = await connect();
const result = await db.collection( 'devices' ).insertOne(device);
return result.ops[0];
} catch (error) {
throw error;
}
}
module.exports = {
getDevices,
createDevice,
};
|
ログイン後にコピー
エントリ ファイルを作成します
- ルート ディレクトリに
index.js
ファイルを作成し、次の内容を追加します:
1 2 3 4 5 6 7 8 9 10 11 12 | const express = require ( 'express' );
const devicesRouter = require ( './routes/devices' );
const app = express();
app. use (express.json());
app. use ( '/devices' , devicesRouter);
app.listen(3000, () => {
console.log( 'Server is running on port 3000' );
});
|
ログイン後にコピー
この時点で、シンプルなスマート ホーム システムの開発が完了しました。ユーザー ログインと登録、デバイス管理、スケジュールされたタスク、操作記録機能。
4. 概要
この記事では、MongoDB を使用してシンプルなスマート ホーム システムを開発する方法を紹介します。 MongoDB と Node.js を組み合わせて使用することで、データの保存と処理を簡単に処理できます。読者はこのシステムをさらに拡張し、特定のニーズに応じて機能を追加できます。
この記事で提供されているコード例は参考用であり、実際の開発中に実際のニーズに応じて修正および改良する必要があります。
以上がMongoDB を使用してシンプルなスマート ホーム システムを開発する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。