{"id":20648157,"url":"https://github.com/miso-develop/opniz-server","last_synced_at":"2025-04-16T14:53:41.474Z","repository":{"id":57315704,"uuid":"434588178","full_name":"miso-develop/opniz-server","owner":"miso-develop","description":null,"archived":false,"fork":false,"pushed_at":"2023-10-17T11:56:59.000Z","size":3005,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T05:24:05.321Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miso-develop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-03T12:27:07.000Z","updated_at":"2023-01-19T10:10:43.000Z","dependencies_parsed_at":"2024-11-19T02:03:19.295Z","dependency_job_id":null,"html_url":"https://github.com/miso-develop/opniz-server","commit_stats":{"total_commits":19,"total_committers":2,"mean_commits":9.5,"dds":0.3157894736842105,"last_synced_commit":"0ff3c84a5a3c67df672d64195260fdce45f940ed"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fopniz-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fopniz-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fopniz-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miso-develop%2Fopniz-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miso-develop","download_url":"https://codeload.github.com/miso-develop/opniz-server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249252513,"owners_count":21238202,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T17:06:39.479Z","updated_at":"2025-04-16T14:53:41.450Z","avatar_url":"https://github.com/miso-develop.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# opniz Server\n\n\u003cdiv align=\"center\"\u003e\u003cimg src=\"https://user-images.githubusercontent.com/22117028/150321839-e1e73dcb-d4b6-4bac-92b9-b1501ff3b092.png\" alt=\"logo\"\u003e\u003c/div\u003e\n\n\u003e ❗ このプロジェクトは現在アルファ版です。\n\nopnizとはM5StackデバイスをNode.js（JavaScript / TypeScript）からobnizライクに実装できるオープンソースフレームワークです。  \nNode.js SDKおよびArduinoライブラリがあり、WebSocketで相互通信を行います。  \n\nしくみとしてはM5StackデバイスおよびNode.js SDK間にてJSON形式のRPCメッセージをやりとりし、相互に定義されたメソッドを呼び合います。  \n\n![overview](https://user-images.githubusercontent.com/22117028/274111780-1f1e22ec-66ac-4cd0-bee3-1a19ee2eb65c.png)\n\nopniz Serverはopniz Node.js SDKやopnizデバイスからのJSON RPCメッセージを中継するWebSocketサーバです。  \nopniz Serverを介すことでWebSocketクライアント同士の接続（opnizデバイス、ブラウザ、Node.js SDK等）を行えます。  \n\n![server-local](https://user-images.githubusercontent.com/22117028/150321900-44d92da2-2b27-4344-9a4f-8c714d85399f.png)\n\nクラウド環境（PaaS、FaaS等）でも動作するため、たとえば手元のPCで動作させたopniz Node.js SDKからインターネット越しの環境にあるopnizデバイスを制御するといったことも可能です。  \n\n![server-global](https://user-images.githubusercontent.com/22117028/150321879-8784f4ef-d344-4d5f-98ff-34b437866dcf.png)\n\n\n\n## インストール方法\n\n```\nnpm install opniz-server\n```\n\n\n\n## 使い方\n\nポート番号を指定するだけでサーバが起動します。  \n\n```js\nconst { OpnizServer } = require(\"opniz-server\")\n\nconst port = 3000\n\nconst server = new OpnizServer(port)\n```\n\nopnizクライアントからはopniz Serverのアドレス、ポート番号に加え、opniz Serverを経由して接続したいopnizデバイスと共通のID（任意の文字列）を指定し接続します。  \nIDは合言葉のようなものでopniz Serverにて同じIDを使用して接続しているopnizクライアント同士をマッチングします。  \n\n以下はNode.js SDKのサンプルコードです。  \nopniz Serverを経由して接続したopnizデバイスのヒープメモリサイズを1秒おきに取得します。  \n\n```js\n// Node.js SDK Client\n\nconst { Opniz } = require(\"opniz\")\n\nconst address = \"127.0.0.1\" // opniz Serverのアドレスを指定\nconst port = 3000           // opniz Serverのポート番号を指定\nconst id = \"1234-5678\"      // opniz Serverを経由して接続したいopnizデバイスで指定したIDと同じ任意の文字列を指定\nconst opniz = new Opniz.Esp32({ address, port, id }) // opnizインスタンス生成\n\n\n\n; (async () =\u003e {\n\twhile (!(await opniz.connect())) { console.log(\"connect...\") } // opniz Serverを経由してopnizデバイスへ接続\n\t\n\tsetInterval(async () =\u003e {\n\t\tconsole.log(await opniz.getFreeHeap()) // opnizデバイスのヒープメモリーサイズを取得して表示\n\t}, 1000)\n})()\n```\n\n以下はopnizデバイス（Arduino Library）のサンプルコードです。  \nNode.js SDKと同様にopniz Serverのアドレス、ポート番号と、Node.js SDKと同じ値となる任意のIDを指定し接続します。  \n\n```cpp\n// Arduino Library Client\n\n#include \u003cOpnizEsp32.h\u003e\n#include \u003clib/WiFiConnector.h\u003e\n\nconst char* ssid = \"\u003cSSID\u003e\";         // Wi-FiのSSIDに書き換え\nconst char* password = \"\u003cPASSWORD\u003e\"; // Wi-Fiのパスワードに書き換え\nWiFiConnector wifiConnector(ssid, password); // Wi-Fi接続ヘルパーインスタンス生成\n\nconst char* address = \"192.168.0.1\"; // opniz Serverのアドレスを指定\nconst uint16_t port = 3000;          // opniz Serverのポート番号を指定\nconst String id = \"1234-5678\";       // opniz Serverを経由して接続したいNode.js SDKで指定したIDと同じ任意の文字列を指定\nOpniz::Esp32* opniz = new Opniz::Esp32(address, port, id); // opnizインスタンス生成\n\n\n\nvoid setup() {\n    wifiConnector.connect(); // Wi-Fi接続\n    opniz-\u003econnect();        // opniz Serverを経由してNode.js SDKへ接続\n}\n\nvoid loop() {\n    opniz-\u003eloop();         // opnizメインループ\n    wifiConnector.watch(); // Wi-Fi接続監視\n}\n```\n\n\n\n### Herokuへデプロイ\n\nクラウド環境へのデプロイの一例として、Herokuへのデプロイコードは以下のようになります。  \nポート番号を`process.env.PORT`と指定することがポイントです。  \n\n```js\nconst { OpnizServer } = require(\"opniz-server\")\n\nconst port = process.env.PORT\n\nconst server = new OpnizServer(port)\n```\n\n\n\n## API\n\nopniz Serverではopnizクライアントからのイベントが発生したタイミングで任意の制御を行えるAPIを用意しています。  \n\n### onconnection / onconnect\n\n`onconnection`、`onconnect`にてopnizクライアントからの接続情報を取得できます。  \n`onconnection`はopnizクライアントからopniz Serverへアクセスしたタイミングで呼び出されます。  \nその後IDが一致するopnizクライアント同士の接続が完了したタイミングで`onconnect`が呼び出されます。  \n\nパラメータの`opnizId`はopnizクライアントにて指定されたIDです。  \n`socket`、`primary`、`secondary`はSocket.IOのSocketオブジェクトです。  \n\n``` js\nconst { OpnizServer } = require(\"opniz-server\")\n\nconst port = 3000\n\nconst server = new OpnizServer(port)\n\n// opnizクライアントからopniz Serverへのアクセス時に呼び出し\nserver.onconnection = (opnizId, socket) =\u003e {\n\tconsole.log(opnizId, socket.id)\n}\n\n// IDが一致するopnizクライアント同士の接続完了時に呼び出し\nserver.onconnect = (opnizId, primary, secondary) =\u003e {\n\tconsole.log(opnizId, primary.id, secondary.id)\n}\n```\n\n### onrequest / onresponse\n\n`onrequest`、`onresponse`にてopnizクライアントからのリクエスト/レスポンスをハンドリングできます。  \n`payload`が生データとなり、`src`が送信元、`dist`送信先のSocket.IOのSocketオブジェクトです。  \nこれによりログを取得したり`payload`の書き換えが行えます。  \n\n```js\nconst { OpnizServer } = require(\"opniz-server\")\n\nconst port = 3000\n\nconst server = new OpnizServer(port)\n\n// opnizクライアントからのリクエストをハンドリング\nserver.onrequest = async (payload, src, dist) =\u003e {\n\tconsole.log(payload)\n\treturn payload\n}\n\n// opnizクライアントからのレスポンスをハンドリング\nserver.onresponse = async (payload, src, dist) =\u003e {\n\tconsole.log(payload)\n\treturn payload\n}\n```\n\n\n\n## ドキュメント\n\n[TypeDoc](https://miso-develop.github.io/opniz-server/)\n\n\n\n## 関連リポジトリ\n\n* [opniz SDK for Node.js](https://github.com/miso-develop/opniz-sdk-nodejs)\n\t* opnizデバイスをNode.jsから遠隔制御するためのSDK\n* [opniz Arduino Library for M5Unified](https://github.com/miso-develop/opniz-arduino-m5unified)\n\t* M5Unified向けArduinoライブラリ\n* [opniz CLI](https://github.com/miso-develop/opniz-cli)\n\t* opniz Arduino LibraryのBasicスケッチをコマンドから簡単に書き込めるCLIツール\n\n\n\n## ライセンス\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiso-develop%2Fopniz-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiso-develop%2Fopniz-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiso-develop%2Fopniz-server/lists"}