{"id":466254,"date":"2025-07-07T15:14:09","date_gmt":"2025-07-07T15:14:09","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=466254"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=466254","title":{"rendered":"<span>\u0421\u0435\u0440\u0432\u0435\u0440 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043a\u0441\u043e\u043c \u0438 \u0441\u0442\u0430\u043d\u0446\u0438\u0435\u0439 \u0434\u0440\u043e\u043d\u0430: \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0438 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h4>\ud83d\udd27 \u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 <strong>\u0443\u0434\u0430\u043b\u0451\u043d\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438<\/strong>, \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0451\u043d\u043d\u044b\u043c\u0438 \u043f\u043e WebSocket. \u041d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u043d\u0435\u043c\u0443 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u043e <strong>\u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/strong>, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442:<\/p>\n<ul>\n<li>\n<p>\u0432\u0438\u0434\u0435\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432;<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e;<\/p>\n<\/li>\n<li>\n<p>\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0431\u043e\u043a\u0441\u0430\u043c \u0438 \u0441\u0442\u0430\u043d\u0446\u0438\u044f\u043c.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u0430\u043f\u0430 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <strong>\u0432 \u0444\u043e\u043d\u043e\u0432\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0431\u0435\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430<\/strong>, \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044f \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b.<\/p>\n<h4>\ud83d\udca1 \u041e\u0431\u0449\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f<\/h4>\n<p>\u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u043d\u0430 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 <strong>\u0442\u0440\u0451\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432<\/strong>:<\/p>\n<ol>\n<li>\n<p><strong>\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430<\/strong> (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>esp01<\/code> \u0438 <code>esp02<\/code>) \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0447\u0435\u0440\u0435\u0437 WebSocket \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0435\u0440\u0432\u0435\u0440 Flask + WebSocket<\/strong>, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON.<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u0435\u0431-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 (\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e)<\/strong> \u2014 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<\/li>\n<\/ol>\n<h4>\u2699\ufe0f \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/h4>\n<h3>1. \u0412\u0435\u0431-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 (Flask)<\/h3>\n<ul>\n<li>\n<p>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c (<code>bcrypt<\/code>, <code>Flask-Login<\/code>).<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u0440\u0448\u0440\u0443\u0442\u044b:<\/p>\n<ul>\n<li>\n<p><code>\/login<\/code>, <code>\/logout<\/code>, <code>\/<\/code> \u2014 \u0432\u0445\u043e\u0434, \u0432\u044b\u0445\u043e\u0434, \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430;<\/p>\n<\/li>\n<li>\n<p><code>\/send_command<\/code> \u2014 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u043a\u043e\u043c\u0430\u043d\u0434 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c;<\/p>\n<\/li>\n<li>\n<p><code>\/get_telemetry<\/code> \u2014 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u0438 \u043e\u0442 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>2. WebSocket-\u0441\u0435\u0440\u0432\u0435\u0440 (\u043d\u0430 websockets)<\/h3>\n<ul>\n<li>\n<p>\u041e\u0436\u0438\u0434\u0430\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0438 \u043f\u0430\u0440\u043e\u043b\u044e.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c <code>authenticated_clients<\/code>, \u0433\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u0431 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f\u0445.<\/p>\n<\/li>\n<\/ul>\n<h3>3. \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043a\u043e\u043c\u0430\u043d\u0434<\/h3>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>send_command_to(...)<\/code> \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 JSON-\u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0451\u043d\u043d\u043e\u043c\u0443 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0443 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435:<\/p>\n<pre><code>json{   \"command\": \"servo1\",   \"value\": \"90\" } <\/code><\/pre>\n<p><code>{   \"command\": \"servo1\",   \"value\": \"90\" } <\/code><\/p>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u043d\u043e \u043e\u0431\u0451\u0440\u043d\u0443\u0442\u044b \u0432 <code>threading + eventlet<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043c\u0435\u0448\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0435 Flask-\u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<h4>\ud83d\uddc3 \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (data.json)<\/h4>\n<pre><code>json\u041a{   \"users\": [     {       \"id\": 1,       \"username\": \"admin\",       \"password_hash\": \"...\"  \/\/ bcrypt-\u0445\u0435\u0448     }   ],   \"devices\": [     {       \"name\": \"esp01\",       \"type\": \"box\",       \"password\": \"1234\",       \"status\": \"offline\"     },     {       \"name\": \"esp02\",       \"type\": \"station\",       \"password\": \"5678\",       \"status\": \"offline\"     }   ] } <\/code><\/pre>\n<p><strong>\u041f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u0435:<\/strong><\/p>\n<ul>\n<li>\n<p><code>users<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<\/li>\n<li>\n<p><code>devices<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0441 \u0442\u0438\u043f\u043e\u043c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>box<\/code>, <code>station<\/code>), \u043f\u0430\u0440\u043e\u043b\u0435\u043c \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0441\u0442\u0430\u0442\u0443\u0441\u043e\u043c (<code>online<\/code>\/<code>offline<\/code>\/<code>\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/code> \u0438 \u0442. \u0434.).<\/p>\n<\/li>\n<\/ul>\n<h4>\ud83d\udce1 \u041f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u043e\u0431\u043c\u0435\u043d\u0430<\/h4>\n<h3>\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430:<\/h3>\n<ul>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 WebSocket.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442:<\/p>\n<\/li>\n<\/ul>\n<pre><code>json{ \"type\": \"auth\", \"name\": \"esp01\", \"password\": \"1234\" }<\/code><\/pre>\n<p><code>{ \"type\": \"auth\", \"name\": \"esp01\", \"password\": \"1234\" } <\/code><\/p>\n<ul>\n<li>\n<p>\u041f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e:<\/p>\n<\/li>\n<\/ul>\n<pre><code>json{   \"type\": \"telemetry\",   \"status\": \"\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\",   \"servo1\": 90,   \"servo2\": 90,   \"button1\": \"released\",   \"button2\": \"pressed\" }<\/code><\/pre>\n<p><code>{   \"type\": \"telemetry\",   \"status\": \"\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\",   \"servo1\": 90,   \"servo2\": 90,   \"button1\": \"released\",   \"button2\": \"pressed\" } <\/code><\/p>\n<h3>\u0421\u0435\u0440\u0432\u0435\u0440:<\/h3>\n<ul>\n<li>\n<p>\u041e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430:<\/p>\n<\/li>\n<\/ul>\n<pre><code>json{ \"command\": \"servo1\", \"value\": \"90\" }<\/code><\/pre>\n<p><code>{ \"command\": \"servo1\", \"value\": \"90\" } <\/code><\/p>\n<h4>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b<\/h4>\n<ul>\n<li>\n<p>\ud83d\udcf1 <strong>\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0432\u0435\u0431-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441<\/strong> \u2014 \u0438\u0434\u0435\u0430\u043b\u0435\u043d \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438, \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043e\u0442\u043b\u0430\u0434\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p>\ud83d\udd0c <strong>WebSocket + JSON<\/strong> \u2014 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043b\u0451\u0433\u043a\u0443\u044e \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e \u0441 \u043c\u0438\u043a\u0440\u043e\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u043c\u0438 \u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u043c\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u043c\u0438.<\/p>\n<\/li>\n<li>\n<p>\ud83e\udde9 <strong>\u0413\u0438\u0431\u043a\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430<\/strong> \u2014 \u043b\u0435\u0433\u043a\u043e \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0434 \u0431\u0443\u0434\u0443\u0449\u0435\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0432\u043d\u0435\u0448\u043d\u0435\u0435 API.<\/p>\n<\/li>\n<\/ul>\n<h4>\u23ed\ufe0f \u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435<\/h4>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u044d\u0442\u0430\u043f\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <strong>\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430<\/strong>, \u0433\u0434\u0435 \u044d\u0442\u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u0442\u0430\u043d\u0435\u0442 <strong>\u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c<\/strong>, \u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <strong>\u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440<\/strong>, \u0431\u0435\u0437 \u0443\u0447\u0430\u0441\u0442\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e.<\/p>\n<h3>\u041a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b <\/h3>\n<pre><code class=\"python\">from flask import Flask, render_template, request, redirect, url_for, flash, jsonify from flask_login import LoginManager, login_user, login_required, logout_user, current_user, UserMixin import bcrypt import asyncio import threading import json import websockets import eventlet import eventlet.wsgi  app = Flask(__name__) app.secret_key = 'supersecretkey123' last_telemetry = {} login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'login'  DATA_FILE = 'data.json'  # \u0427\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0445 def load_data():     with open(DATA_FILE, 'r', encoding='utf-8') as f:         return json.load(f)  def save_data(data):     with open(DATA_FILE, 'w', encoding='utf-8') as f:         json.dump(data, f, indent=2, ensure_ascii=False)  # \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0434\u043b\u044f Flask-Login class User(UserMixin):     def __init__(self, id_, username):         self.id = id_         self.username = username  @login_manager.user_loader def load_user(user_id):     data = load_data()     for u in data['users']:         if str(u['id']) == user_id:             return User(u['id'], u['username'])     return None  @app.route('\/login', methods=['GET', 'POST']) def login():     if request.method == 'POST':         username = request.form['username']         password = request.form['password'].encode('utf-8')          data = load_data()         for u in data['users']:             if u['username'] == username and bcrypt.checkpw(password, u['password_hash'].encode('utf-8')):                 user = User(u['id'], username)                 login_user(user)                 return redirect(url_for('index'))         flash(\"\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u043b\u043e\u0433\u0438\u043d \u0438\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c\")         return redirect(url_for('login'))      return render_template('login.html')  @app.route('\/logout') @login_required def logout():     logout_user()     return redirect(url_for('login'))  @app.route('\/') @login_required def index():     data = load_data()     devices = [(d['name'], d['status']) for d in data['devices']]     return render_template('index.html', devices=devices, username=current_user.username)  authenticated_clients = {}  async def ws_handler(websocket):     name = None     try:         message = await websocket.recv()         print(f\"message {message}\")         data = json.loads(message)         if data.get('type') != 'auth':             await websocket.send(json.dumps({\"error\": \"auth_required\"}))             return          name = data.get('name')         password = data.get('password')          all_data = load_data()         device = next((d for d in all_data['devices'] if d['name'] == name), None)          if not device or device['password'] != password:             await websocket.send(json.dumps({\"error\": \"unauthorized\"}))             return          print(f\"\u2705 \u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e {name} \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u043e\u0441\u044c\")         authenticated_clients[name] = websocket          # \u041e\u0431\u043d\u043e\u0432\u0438\u043c \u0441\u0442\u0430\u0442\u0443\u0441 \u0432 JSON         device['status'] = 'online'         save_data(all_data)          async for message in websocket:             data = json.loads(message)             if data.get('type') == 'telemetry':                 status = data.get('status', 'unknown')                 print(f\"\ud83d\udce1 \u0422\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044f \u043e\u0442 {name}: {status}\")                 device['status'] = status                 last_telemetry[name] = data                  save_data(all_data)      except websockets.exceptions.ConnectionClosed:         print(f\"\u274c {name} \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u043b\u0441\u044f\")     finally:         if name:             authenticated_clients.pop(name, None)             all_data = load_data()             device = next((d for d in all_data['devices'] if d['name'] == name), None)             if device:                 device['status'] = 'offline'                 save_data(all_data)  # \u041d\u043e\u0432\u044b\u0439 \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u0438 @app.route('\/get_telemetry') @login_required def get_telemetry():     device_name = request.args.get('device')     if device_name in last_telemetry:         return jsonify(last_telemetry[device_name])     return jsonify({\"error\": \"no data\"})  async def send_command_to(name, command, value=None):  # \u0414\u0435\u043b\u0430\u0435\u043c value \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c     ws = authenticated_clients.get(name)     if not ws:         return False          # \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u044f value     if value is not None:         cmd_json = json.dumps({             \"command\": command,             \"value\": str(value)         })     else:         cmd_json = json.dumps({             \"command\": command         })          await ws.send(cmd_json)     return True @app.route('\/send_command', methods=['POST']) @login_required def send_command():     name = request.form.get('device_name')     command = request.form.get('command')     value = request.form.get('value', None)  # \u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 value          if not name or not command:         flash(\"\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u043b\u044f\")         return redirect(url_for('index'))          try:         loop = asyncio.new_event_loop()         asyncio.set_event_loop(loop)         success = loop.run_until_complete(send_command_to(name, command, value))         loop.close()                  if success:             flash(f\"\u041a\u043e\u043c\u0430\u043d\u0434\u0430 {command} \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0430\")         else:             flash(\"\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e offline\")     except Exception as e:         flash(f\"\u041e\u0448\u0438\u0431\u043a\u0430: {str(e)}\")          return redirect(url_for('index'))    def start_websocket_server():     loop = asyncio.new_event_loop()     asyncio.set_event_loop(loop)          async def server():         async with websockets.serve(ws_handler, '0.0.0.0', 8765):             await asyncio.Future()  # run forever          loop.run_until_complete(server())  if __name__ == '__main__':     # \u0417\u0430\u043f\u0443\u0441\u043a WebSocket \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435     t = threading.Thread(target=start_websocket_server, daemon=True)     t.start()          # \u0417\u0430\u043f\u0443\u0441\u043a Flask \u0441\u0435\u0440\u0432\u0435\u0440\u0430     eventlet.wsgi.server(eventlet.listen(('', 5000)), app) <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/923820\/\"> https:\/\/habr.com\/ru\/articles\/923820\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h4>\ud83d\udd27 \u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 <strong>\u0443\u0434\u0430\u043b\u0451\u043d\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438<\/strong>, \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0451\u043d\u043d\u044b\u043c\u0438 \u043f\u043e WebSocket. \u041d\u0430 \u044d\u0442\u0430\u043f\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u043d\u0435\u043c\u0443 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u043e <strong>\u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/strong>, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442:<\/p>\n<ul>\n<li>\n<p>\u0432\u0438\u0434\u0435\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432;<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e;<\/p>\n<\/li>\n<li>\n<p>\u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0431\u043e\u043a\u0441\u0430\u043c \u0438 \u0441\u0442\u0430\u043d\u0446\u0438\u044f\u043c.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u0430\u043f\u0430 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <strong>\u0432 \u0444\u043e\u043d\u043e\u0432\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0431\u0435\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430<\/strong>, \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044f \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b.<\/p>\n<h4>\ud83d\udca1 \u041e\u0431\u0449\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f<\/h4>\n<p>\u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u043d\u0430 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 <strong>\u0442\u0440\u0451\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432<\/strong>:<\/p>\n<ol>\n<li>\n<p><strong>\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430<\/strong> (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>esp01<\/code> \u0438 <code>esp02<\/code>) \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0447\u0435\u0440\u0435\u0437 WebSocket \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0435\u0440\u0432\u0435\u0440 Flask + WebSocket<\/strong>, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 JSON.<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u0435\u0431-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 (\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e)<\/strong> \u2014 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0443 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<\/li>\n<\/ol>\n<h4>\u2699\ufe0f \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/h4>\n<h3>1. \u0412\u0435\u0431-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 (Flask)<\/h3>\n<ul>\n<li>\n<p>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0447\u0435\u0440\u0435\u0437 \u043b\u043e\u0433\u0438\u043d\/\u043f\u0430\u0440\u043e\u043b\u044c (<code>bcrypt<\/code>, <code>Flask-Login<\/code>).<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u0440\u0448\u0440\u0443\u0442\u044b:<\/p>\n<ul>\n<li>\n<p><code>\/login<\/code>, <code>\/logout<\/code>, <code>\/<\/code> \u2014 \u0432\u0445\u043e\u0434, \u0432\u044b\u0445\u043e\u0434, \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430;<\/p>\n<\/li>\n<li>\n<p><code>\/send_command<\/code> \u2014 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u043a\u043e\u043c\u0430\u043d\u0434 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c;<\/p>\n<\/li>\n<li>\n<p><code>\/get_telemetry<\/code> \u2014 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u0438 \u043e\u0442 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430.<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>2. WebSocket-\u0441\u0435\u0440\u0432\u0435\u0440 (\u043d\u0430 websockets)<\/h3>\n<ul>\n<li>\n<p>\u041e\u0436\u0438\u0434\u0430\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0438 \u043f\u0430\u0440\u043e\u043b\u044e.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c <code>authenticated_clients<\/code>, \u0433\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u0431 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f\u0445.<\/p>\n<\/li>\n<\/ul>\n<h3>3. \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043a\u043e\u043c\u0430\u043d\u0434<\/h3>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>send_command_to(...)<\/code> \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 JSON-\u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0451\u043d\u043d\u043e\u043c\u0443 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0443 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435:<\/p>\n<pre><code>json{   \"command\": \"servo1\",   \"value\": \"90\" } <\/code><\/pre>\n<p><code>{   \"command\": \"servo1\",   \"value\": \"90\" } <\/code><\/p>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u043d\u043e \u043e\u0431\u0451\u0440\u043d\u0443\u0442\u044b \u0432 <code>threading + eventlet<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043c\u0435\u0448\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0435 Flask-\u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<h4>\ud83d\uddc3 \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (data.json)<\/h4>\n<pre><code>json\u041a{   \"users\": [     {       \"id\": 1,       \"username\": \"admin\",       \"password_hash\": \"...\"  \/\/ bcrypt-\u0445\u0435\u0448     }   ],   \"devices\": [     {       \"name\": \"esp01\",       \"type\": \"box\",       \"password\": \"1234\",       \"status\": \"offline\"     },     {       \"name\": \"esp02\",       \"type\": \"station\",       \"password\": \"5678\",       \"status\": \"offline\"     }   ] } <\/code><\/pre>\n<p><strong>\u041f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u0435:<\/strong><\/p>\n<ul>\n<li>\n<p><code>users<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.<\/p>\n<\/li>\n<li>\n<p><code>devices<\/code> \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0441 \u0442\u0438\u043f\u043e\u043c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>box<\/code>, <code>station<\/code>), \u043f\u0430\u0440\u043e\u043b\u0435\u043c \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0441\u0442\u0430\u0442\u0443\u0441\u043e\u043c (<code>online<\/code>\/<code>offline<\/code>\/<code>\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/code> \u0438 \u0442. \u0434.).<\/p>\n<\/li>\n<\/ul>\n<h4>\ud83d\udce1 \u041f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u043e\u0431\u043c\u0435\u043d\u0430<\/h4>\n<h3>\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430:<\/h3>\n<ul>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 WebSocket.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442:<\/p>\n<\/li>\n<\/ul>\n<pre><code>json{ \"type\": \"auth\", \"name\": \"esp01\", \"password\": \"1234\" }<\/code><\/pre>\n<p><code>{ \"type\": \"auth\", \"name\": \"esp01\", \"password\": \"1234\" } <\/code><\/p>\n<ul>\n<li>\n<p>\u041f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442 \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044e:<\/p>\n<\/li>\n<\/ul>\n<pre><code>json{   \"type\": \"telemetry\",   \"status\": \"\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\",   \"servo1\": 90,   \"servo2\": 90,   \"button1\": \"released\",   \"button2\": \"pressed\" }<\/code><\/pre>\n<p><code>{   \"type\": \"telemetry\",   \"status\": \"\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\",   \"servo1\": 90,   \"servo2\": 90,   \"button1\": \"released\",   \"button2\": \"pressed\" } <\/code><\/p>\n<h3>\u0421\u0435\u0440\u0432\u0435\u0440:<\/h3>\n<ul>\n<li>\n<p>\u041e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438 \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430:<\/p>\n<\/li>\n<\/ul>\n<pre><code>json{ \"command\": \"servo1\", \"value\": \"90\" }<\/code><\/pre>\n<p><code>{ \"command\": \"servo1\", \"value\": \"90\" } <\/code><\/p>\n<h4>\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b<\/h4>\n<ul>\n<li>\n<p>\ud83d\udcf1 <strong>\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0432\u0435\u0431-\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441<\/strong> \u2014 \u0438\u0434\u0435\u0430\u043b\u0435\u043d \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438, \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043e\u0442\u043b\u0430\u0434\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p>\ud83d\udd0c <strong>WebSocket + JSON<\/strong> \u2014 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043b\u0451\u0433\u043a\u0443\u044e \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044e \u0441 \u043c\u0438\u043a\u0440\u043e\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u043c\u0438 \u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u043c\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u043c\u0438.<\/p>\n<\/li>\n<li>\n<p>\ud83e\udde9 <strong>\u0413\u0438\u0431\u043a\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430<\/strong> \u2014 \u043b\u0435\u0433\u043a\u043e \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0434 \u0431\u0443\u0434\u0443\u0449\u0435\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0432\u043d\u0435\u0448\u043d\u0435\u0435 API.<\/p>\n<\/li>\n<\/ul>\n<h4>\u23ed\ufe0f \u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435<\/h4>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u044d\u0442\u0430\u043f\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <strong>\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430<\/strong>, \u0433\u0434\u0435 \u044d\u0442\u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u0442\u0430\u043d\u0435\u0442 <strong>\u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u043c<\/strong>, \u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <strong>\u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440<\/strong>, \u0431\u0435\u0437 \u0443\u0447\u0430\u0441\u0442\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e.<\/p>\n<h3>\u041a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b <\/h3>\n<pre><code class=\"python\">from flask import Flask, render_template, request, redirect, url_for, flash, jsonify from flask_login import LoginManager, login_user, login_required, logout_user, current_user, UserMixin import bcrypt import asyncio import threading import json import websockets import eventlet import eventlet.wsgi  app = Flask(__name__) app.secret_key = 'supersecretkey123' last_telemetry = {} login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'login'  DATA_FILE = 'data.json'  # \u0427\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u0430\u043d\u043d\u044b\u0445 def load_data():     with open(DATA_FILE, 'r', encoding='utf-8') as f:         return json.load(f)  def save_data(data):     with open(DATA_FILE, 'w', encoding='utf-8') as f:         json.dump(data, f, indent=2, ensure_ascii=False)  # \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0434\u043b\u044f Flask-Login class User(UserMixin):     def __init__(self, id_, username):         self.id = id_         self.username = username  @login_manager.user_loader def load_user(user_id):     data = load_data()     for u in data['users']:         if str(u['id']) == user_id:             return User(u['id'], u['username'])     return None  @app.route('\/login', methods=['GET', 'POST']) def login():     if request.method == 'POST':         username = request.form['username']         password = request.form['password'].encode('utf-8')          data = load_data()         for u in data['users']:             if u['username'] == username and bcrypt.checkpw(password, u['password_hash'].encode('utf-8')):                 user = User(u['id'], username)                 login_user(user)                 return redirect(url_for('index'))         flash(\"\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u043b\u043e\u0433\u0438\u043d \u0438\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c\")         return redirect(url_for('login'))      return render_template('login.html')  @app.route('\/logout') @login_required def logout():     logout_user()     return redirect(url_for('login'))  @app.route('\/') @login_required def index():     data = load_data()     devices = [(d['name'], d['status']) for d in data['devices']]     return render_template('index.html', devices=devices, username=current_user.username)  authenticated_clients = {}  async def ws_handler(websocket):     name = None     try:         message = await websocket.recv()         print(f\"message {message}\")         data = json.loads(message)         if data.get('type') != 'auth':             await websocket.send(json.dumps({\"error\": \"auth_required\"}))             return          name = data.get('name')         password = data.get('password')          all_data = load_data()         device = next((d for d in all_data['devices'] if d['name'] == name), None)          if not device or device['password'] != password:             await websocket.send(json.dumps({\"error\": \"unauthorized\"}))             return          print(f\"\u2705 \u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e {name} \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u043e\u0441\u044c\")         authenticated_clients[name] = websocket          # \u041e\u0431\u043d\u043e\u0432\u0438\u043c \u0441\u0442\u0430\u0442\u0443\u0441 \u0432 JSON         device['status'] = 'online'         save_data(all_data)          async for message in websocket:             data = json.loads(message)             if data.get('type') == 'telemetry':                 status = data.get('status', 'unknown')                 print(f\"\ud83d\udce1 \u0422\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u044f \u043e\u0442 {name}: {status}\")                 device['status'] = status                 last_telemetry[name] = data                  save_data(all_data)      except websockets.exceptions.ConnectionClosed:         print(f\"\u274c {name} \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u043b\u0441\u044f\")     finally:         if name:             authenticated_clients.pop(name, None)             all_data = load_data()             device = next((d for d in all_data['devices'] if d['name'] == name), None)             if device:                 device['status'] = 'offline'                 save_data(all_data)  # \u041d\u043e\u0432\u044b\u0439 \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043b\u0435\u043c\u0435\u0442\u0440\u0438\u0438 @app.route('\/get_telemetry') @login_required def get_telemetry():     device_name = request.args.get('device')     if device_name in last_telemetry:         return jsonify(last_telemetry[device_name])     return jsonify({\"error\": \"no data\"})  async def send_command_to(name, command, value=None):  # \u0414\u0435\u043b\u0430\u0435\u043c value \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c     ws = authenticated_clients.get(name)     if not ws:         return False          # \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u044f value     if value is not None:         cmd_json = json.dumps({             \"command\": command,             \"value\": str(value)         })     else:         cmd_json = json.dumps({             \"command\": command         })          await ws.send(cmd_json)     return True @app.route('\/send_command', methods=['POST']) @login_required def send_command():     name = request.form.get('device_name')     command = request.form.get('command')     value = request.form.get('value', None)  # \u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 value          if not name or not command:         flash(\"\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u043b\u044f\")         return redirect(url_for('index'))          try:         loop = asyncio.new_event_loop()         asyncio.set_event_loop(loop)         success = loop.run_until_complete(send_command_to(name, command, value))         loop.close()                  if success:             flash(f\"\u041a\u043e\u043c\u0430\u043d\u0434\u0430 {command} \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0430\")         else:             flash(\"\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e offline\")     except Exception as e:         flash(f\"\u041e\u0448\u0438\u0431\u043a\u0430: {str(e)}\")          return redirect(url_for('index'))    def start_websocket_server():     loop = asyncio.new_event_loop()     asyncio.set_event_loop(loop)          async def server():         async with websockets.serve(ws_handler, '0.0.0.0', 8765):             await asyncio.Future()  # run forever          loop.run_until_complete(server())  if __name__ == '__main__':     # \u0417\u0430\u043f\u0443\u0441\u043a WebSocket \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435     t = threading.Thread(target=start_websocket_server, daemon=True)     t.start()          # \u0417\u0430\u043f\u0443\u0441\u043a Flask \u0441\u0435\u0440\u0432\u0435\u0440\u0430     eventlet.wsgi.server(eventlet.listen(('', 5000)), app) <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/923820\/\"> https:\/\/habr.com\/ru\/articles\/923820\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-466254","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/466254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=466254"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/466254\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=466254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=466254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=466254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}