{"id":332731,"date":"2022-05-04T15:00:10","date_gmt":"2022-05-04T15:00:10","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=332731"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=332731","title":{"rendered":"<span>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u043c\u043d\u044b\u043c \u0434\u043e\u043c\u043e\u043c \u043d\u0430 kotlin<\/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<h3>\u041a\u0440\u0430\u0442\u043a\u043e<\/h3>\n<p>Smart System \u044d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430 \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0435 \u043d\u0430 kotlin.<br \/> \u0426\u0435\u043b\u044c \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u2014 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0430\u0447\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0435\u0439 \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430.<\/p>\n<p>\u0422\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0442\u0435\u043a \u0441\u0430\u043c\u044b\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 &#8212; kotlin(backend), xml(frontend).<\/p>\n<h3>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430<\/h3>\n<p>Smart System \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0432\u0430\u0439\u0441\u044b:<\/p>\n<ul>\n<li>\n<p>Philips Hue Bridge<\/p>\n<\/li>\n<li>\n<p>Shelly<\/p>\n<\/li>\n<li>\n<p>Devices using ESP Easy<\/p>\n<\/li>\n<li>\n<p>Devices using Tasmota<\/p>\n<\/li>\n<li>\n<p>Devices using the SimpleHome API<\/p>\n<\/li>\n<\/ul>\n<h2>Activities<\/h2>\n<p>\u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0435\u043d\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430.<\/p>\n<p>\u0412 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043a\u043d\u043e\u043f\u043a\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0441\u0440\u0430\u0437\u0443.<\/p>\n<p>\u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043c\u044f , \u0430\u0434\u0440\u0435\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 , \u0432\u044b\u0431\u043e\u0440 \u0442\u0438\u043f\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0438\u043a\u043e\u043d\u043a\u0438 \u0438 \u0432\u044b\u0431\u043e\u0440 \u043c\u043e\u0434\u0443\u043b\u0435\u0439.<\/p>\n<h3>\u0421\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u044b<\/h3>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/hd\/dd\/da\/hdddda54anyty0_uq2cbqvuzosk.png\" alt=\"          \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d                                        \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438                                    \u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430\" title=\"          \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d                                        \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438                                    \u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430\" width=\"auto\" height=\"auto\" data-src=\"https:\/\/habrastorage.org\/webt\/hd\/dd\/da\/hdddda54anyty0_uq2cbqvuzosk.png\"\/><figcaption>          \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d                                        \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438                                    \u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/figcaption><\/figure>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/qm\/dv\/pf\/qmdvpfabr222olw-o5zkk4pcuvu.png\" alt=\"          \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d                                        \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438                                    \u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430\" title=\"          \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d                                        \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438                                    \u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430\" width=\"auto\" height=\"auto\" data-src=\"https:\/\/habrastorage.org\/webt\/qm\/dv\/pf\/qmdvpfabr222olw-o5zkk4pcuvu.png\"\/><figcaption>          \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d                                        \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438                                    \u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/figcaption><\/figure>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"\" width=\"0\" height=\"0\"\/><figcaption><\/figcaption><\/figure>\n<h2>Backend<\/h2>\n<p>\u042f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u044e\u0441\u044c \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u043d\u0430 \u0441\u0430\u043c\u044b\u0445 \u0432\u0430\u0436\u043d\u044b\u0445 \u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u043a\u043e\u0434\u0430 &#8212; \u044d\u0442\u043e RETROFIT. \u041a\u0430\u043a \u044f \u043f\u0438\u0441\u0430\u043b \u0432 \u0441\u0430\u043c\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0435, \u043c\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438(ESP, Hue, Shelly, SimpleHome, Tasmota). \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 resources \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f JSON \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u044d\u0442\u0438\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439. \u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 ESPeasy, \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u0441\u0443\u0442\u044c \u0432\u0441\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 API.\u0451<\/p>\n<p>\u0412 \u0444\u0430\u0439\u043b\u0435 EspEasyAPI.kt \u0438 EspEasyAPIParser.kt (\u0414\u0443\u043c\u0430\u044e \u044d\u0442\u0438 \u043d\u0435\u0437\u0430\u043c\u044b\u0441\u043b\u043e\u0432\u0430\u0442\u044b\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0433\u043e\u0432\u043e\u0440\u044f\u0442 \u0441\u0430\u043c\u0438 \u0437\u0430 \u0441\u0435\u0431\u044f)<\/p>\n<pre><code class=\"kotlin\">class EspEasyAPI(     c: Context,     deviceId: String,     recyclerViewInterface: HomeRecyclerViewHelperInterface? ) : UnifiedAPI(c, deviceId, recyclerViewInterface) {      private val parser = EspEasyAPIParser(c.resources, this)      override fun loadList(callback: CallbackInterface) {         val jsonObjectRequest = JsonObjectRequest(             Request.Method.GET, url + \"json\", null,             { infoResponse ->                 callback.onItemsLoaded(                     UnifiedRequestCallback(                         parser.parseResponse(infoResponse),                         deviceId                     ),                     recyclerViewInterface                 )             },             { error ->                 callback.onItemsLoaded(                     UnifiedRequestCallback(null, deviceId,                     Global.volleyError(c, error)                 ), null)             }         )         queue.add(jsonObjectRequest)     }      override fun loadStates(callback: RealTimeStatesCallback, offset: Int) {         val jsonObjectRequest = JsonObjectRequest(             Request.Method.GET, url + \"json\", null,             { infoResponse ->                 callback.onStatesLoaded(                     parser.parseStates(infoResponse),                     offset,                     dynamicSummaries                 )             }, { }         )         queue.add(jsonObjectRequest)     }      override fun changeSwitchState(id: String, state: Boolean) {         val switchUrl = url + \"control?cmd=GPIO,\" + id + \",\" + (if (state) \"1\" else \"0\")         val jsonObjectRequest = JsonObjectRequest(             switchUrl,             { },             { e -> Log.e(Global.LOG_TAG, e.toString()) }         )         queue.add(jsonObjectRequest)     } }<\/code><\/pre>\n<pre><code class=\"kotlin\">class EspEasyAPIParser(resources: Resources, api: UnifiedAPI?) : UnifiedAPI.Parser(resources, api) {      override fun parseResponse(response: JSONObject): ArrayList&lt;ListViewItem> {         val listItems = arrayListOf&lt;ListViewItem>()          \/\/sensors         val sensors = response.optJSONArray(\"Sensors\") ?: JSONArray()         for (sensorId in 0 until sensors.length()) {             val currentSensor = sensors.getJSONObject(sensorId)             if (currentSensor.optString(\"TaskEnabled\", \"false\").equals(\"false\")) {                 continue             }              val type = currentSensor.optString(\"Type\")             if (type.startsWith(\"Environment\")) {                 parseEnvironment(listItems, type, currentSensor)             } else if (type.startsWith(\"Switch\")) {                 parseSwitch(listItems, type, currentSensor)             }         }          return listItems     }      private fun parseEnvironment(listItems: ArrayList&lt;ListViewItem>, type: String, currentSensor: JSONObject)  {         var taskIcons = intArrayOf()         when (type) {             \"Environment - BMx280\" -> {                 taskIcons += R.drawable.ic_device_thermometer                 taskIcons += R.drawable.ic_device_hygrometer                 taskIcons += R.drawable.ic_device_gauge             }             \"Environment - DHT11\/12\/22  SONOFF2301\/7021\" -> {                 taskIcons += R.drawable.ic_device_thermometer                 taskIcons += R.drawable.ic_device_hygrometer             }             \"Environment - DS18b20\" -> {                 taskIcons += R.drawable.ic_device_thermometer             }         }          val taskName = currentSensor.getString(\"TaskName\")         for (taskId in taskIcons.indices) {             val currentTask = currentSensor.getJSONArray(\"TaskValues\").getJSONObject(taskId)             val currentValue = currentTask.getString(\"Value\")             if (!currentValue.equals(\"nan\")) {                 val suffix = when (taskIcons[taskId]) {                     R.drawable.ic_device_thermometer -> \" \u00b0C\"                     R.drawable.ic_device_hygrometer -> \" %\"                     R.drawable.ic_device_gauge -> \" hPa\"                     else -> \"\"                 }                 listItems += ListViewItem(                     title = currentValue + suffix,                     summary = taskName + \": \" + currentTask.getString(\"Name\"),                     icon = taskIcons[taskId]                 )             }         }     }      private fun parseSwitch(listItems: ArrayList&lt;ListViewItem>, type: String, currentSensor: JSONObject) {         when (type) {             \"Switch input - Switch\" -> {                 val currentState = currentSensor.getJSONArray(\"TaskValues\").getJSONObject(0).getInt(\"Value\") > 0                 var taskName =currentSensor.getString(\"TaskName\")                 var gpioId = \"\"                 val gpioFinder = Regex(\"~GPIO~([0-9]+)$\")                 val matchResult = gpioFinder.find(taskName)                 if (matchResult != null &amp;&amp; matchResult.groupValues.size > 1) {                     gpioId = matchResult.groupValues[1]                     taskName = taskName.replace(\"~GPIO~$gpioId\", \"\")                 }                 listItems += ListViewItem(                     title = taskName,                     summary = resources.getString(                         if (currentState) R.string.switch_summary_on                         else R.string.switch_summary_off                     ),                     hidden = gpioId,                     state = currentState,                     icon = R.drawable.ic_do                 )                 api?.needsRealTimeData = true             }         }     }      override fun parseStates(response: JSONObject): ArrayList&lt;Boolean?> {         val listItems = arrayListOf&lt;Boolean?>()          \/\/sensors         val sensors = response.optJSONArray(\"Sensors\") ?: JSONArray()         for (sensorId in 0 until sensors.length()) {             val currentSensor = sensors.getJSONObject(sensorId)             if (currentSensor.optString(\"TaskEnabled\", \"false\").equals(\"false\")) {                 continue             }              val type = currentSensor.optString(\"Type\")             if (type.startsWith(\"Environment\")) {                 parseEnvironmentStates(listItems, type, currentSensor)             } else if (type.startsWith(\"Switch\")) {                 parseSwitchStates(listItems, type, currentSensor)             }         }          return listItems     }      private fun parseEnvironmentStates(listItems: ArrayList&lt;Boolean?>, type: String, currentSensor: JSONObject)  {         var tasks = 0         when (type) {             \"Environment - BMx280\" -> tasks += 3             \"Environment - DHT11\/12\/22  SONOFF2301\/7021\" -> tasks += 2             \"Environment - DS18b20\" -> tasks++         }          for (taskId in 0 until tasks) {             if (                 !currentSensor.getJSONArray(\"TaskValues\")                     .getJSONObject(taskId)                     .getString(\"Value\")                     .equals(\"nan\")             ) listItems += null         }     }      private fun parseSwitchStates(listItems: ArrayList&lt;Boolean?>, type: String, currentSensor: JSONObject) {         when (type) {             \"Switch input - Switch\" -> {                 listItems += currentSensor.getJSONArray(\"TaskValues\").getJSONObject(0).getInt(\"Value\") > 0             }         }     } }<\/code><\/pre>\n<h2>\u0415\u0441\u043b\u0438 \u043a\u043e\u043c\u0443-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043a\u0430\u043a \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u0441\u043f\u0435\u043a\u0442\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 kotlin, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043a\u043e\u0434\u043e\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 \u043d\u0438\u0436\u0435.<\/h2>\n<div class=\"embed_link\">\n<div class=\"embed__thumb\" style=\"background-image: url(&quot;https:\/\/opengraph.githubassets.com\/2ee6a9a14c59f85a776075fb2f4bd22c16c25fa2df27c80500328fc3c0502378\/qwoticreator\/SmartSystem&quot;);\"><\/div>\n<div class=\"embed__caption\">\n<div class=\"embed__caption-title\"><span>GitHub &#8212; qwoticreator\/SmartSystem<\/span><\/div>\n<p><a href=\"https:\/\/github.com\/qwoticreator\/SmartSystem\" target=\"_blank\" rel=\"noopener noreferrer nofollow\" class=\"embed__caption-link\">github.com<\/a><\/div>\n<\/div>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043e \u0437\u0430 \u0434\u0432\u0430 \u0434\u043d\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0445\u0430\u043a\u0430\u0442\u043e\u043d\u0430 be-coder \u0441 \u0442\u0435\u043c\u0430\u0442\u0438\u043a\u043e\u0439 \u0421\u0438\u0441\u0442\u0435\u043c\u044b \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u043a\u043e\u0434\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u043d\u0435\u0431\u0440\u0435\u0436\u043d\u043e \u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0442\u0430\u043a \u0447\u0442\u043e \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u044b\u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0441\u043e\u0432\u0435\u0442\u044b \u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u043a\u0440\u0438\u0442\u0438\u043a\u0443!)<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/post\/664312\/\"> https:\/\/habr.com\/ru\/post\/664312\/<\/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<h3>\u041a\u0440\u0430\u0442\u043a\u043e<\/h3>\n<p>Smart System \u044d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430 \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0435 \u043d\u0430 kotlin.<br \/> \u0426\u0435\u043b\u044c \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u2014 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0434\u043b\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0430\u0447\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0435\u0439 \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430.<\/p>\n<p>\u0422\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0442\u0435\u043a \u0441\u0430\u043c\u044b\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 &#8212; kotlin(backend), xml(frontend).<\/p>\n<h3>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430<\/h3>\n<p>Smart System \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0432\u0430\u0439\u0441\u044b:<\/p>\n<ul>\n<li>\n<p>Philips Hue Bridge<\/p>\n<\/li>\n<li>\n<p>Shelly<\/p>\n<\/li>\n<li>\n<p>Devices using ESP Easy<\/p>\n<\/li>\n<li>\n<p>Devices using Tasmota<\/p>\n<\/li>\n<li>\n<p>Devices using the SimpleHome API<\/p>\n<\/li>\n<\/ul>\n<h2>Activities<\/h2>\n<p>\u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0435\u043d\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430.<\/p>\n<p>\u0412 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043a\u043d\u043e\u043f\u043a\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0441\u0440\u0430\u0437\u0443.<\/p>\n<p>\u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u043c\u044f , \u0430\u0434\u0440\u0435\u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 , \u0432\u044b\u0431\u043e\u0440 \u0442\u0438\u043f\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0438\u043a\u043e\u043d\u043a\u0438 \u0438 \u0432\u044b\u0431\u043e\u0440 \u043c\u043e\u0434\u0443\u043b\u0435\u0439.<\/p>\n<h3>\u0421\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u044b<\/h3>\n<figure class=\"\"><figcaption>          \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d                                        \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438                                    \u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/figcaption><\/figure>\n<figure class=\"\"><figcaption>          \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u044d\u043a\u0440\u0430\u043d                                        \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438                                    \u042d\u043a\u0440\u0430\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430<\/figcaption><\/figure>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h2>Backend<\/h2>\n<p>\u042f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u044e\u0441\u044c \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435 \u043d\u0430 \u0441\u0430\u043c\u044b\u0445 \u0432\u0430\u0436\u043d\u044b\u0445 \u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u044f\u0445 \u043a\u043e\u0434\u0430 &#8212; \u044d\u0442\u043e RETROFIT. \u041a\u0430\u043a \u044f \u043f\u0438\u0441\u0430\u043b \u0432 \u0441\u0430\u043c\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0435, \u043c\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438(ESP, Hue, Shelly, SimpleHome, Tasmota). \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043f\u0430\u043a\u0435\u0442\u0435 resources \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f JSON \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u044d\u0442\u0438\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439. \u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 ESPeasy, \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u0441\u0443\u0442\u044c \u0432\u0441\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 API.\u0451<\/p>\n<p>\u0412 \u0444\u0430\u0439\u043b\u0435 EspEasyAPI.kt \u0438 EspEasyAPIParser.kt (\u0414\u0443\u043c\u0430\u044e \u044d\u0442\u0438 \u043d\u0435\u0437\u0430\u043c\u044b\u0441\u043b\u043e\u0432\u0430\u0442\u044b\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0433\u043e\u0432\u043e\u0440\u044f\u0442 \u0441\u0430\u043c\u0438 \u0437\u0430 \u0441\u0435\u0431\u044f)<\/p>\n<pre><code class=\"kotlin\">class EspEasyAPI(     c: Context,     deviceId: String,     recyclerViewInterface: HomeRecyclerViewHelperInterface? ) : UnifiedAPI(c, deviceId, recyclerViewInterface) {      private val parser = EspEasyAPIParser(c.resources, this)      override fun loadList(callback: CallbackInterface) {         val jsonObjectRequest = JsonObjectRequest(             Request.Method.GET, url + \"json\", null,             { infoResponse ->                 callback.onItemsLoaded(                     UnifiedRequestCallback(                         parser.parseResponse(infoResponse),                         deviceId                     ),                     recyclerViewInterface                 )             },             { error ->                 callback.onItemsLoaded(                     UnifiedRequestCallback(null, deviceId,                     Global.volleyError(c, error)                 ), null)             }         )         queue.add(jsonObjectRequest)     }      override fun loadStates(callback: RealTimeStatesCallback, offset: Int) {         val jsonObjectRequest = JsonObjectRequest(             Request.Method.GET, url + \"json\", null,             { infoResponse ->                 callback.onStatesLoaded(                     parser.parseStates(infoResponse),                     offset,                     dynamicSummaries                 )             }, { }         )         queue.add(jsonObjectRequest)     }      override fun changeSwitchState(id: String, state: Boolean) {         val switchUrl = url + \"control?cmd=GPIO,\" + id + \",\" + (if (state) \"1\" else \"0\")         val jsonObjectRequest = JsonObjectRequest(             switchUrl,             { },             { e -> Log.e(Global.LOG_TAG, e.toString()) }         )         queue.add(jsonObjectRequest)     } }<\/code><\/pre>\n<pre><code class=\"kotlin\">class EspEasyAPIParser(resources: Resources, api: UnifiedAPI?) : UnifiedAPI.Parser(resources, api) {      override fun parseResponse(response: JSONObject): ArrayList&lt;ListViewItem> {         val listItems = arrayListOf&lt;ListViewItem>()          \/\/sensors         val sensors = response.optJSONArray(\"Sensors\") ?: JSONArray()         for (sensorId in 0 until sensors.length()) {             val currentSensor = sensors.getJSONObject(sensorId)             if (currentSensor.optString(\"TaskEnabled\", \"false\").equals(\"false\")) {                 continue             }              val type = currentSensor.optString(\"Type\")             if (type.startsWith(\"Environment\")) {                 parseEnvironment(listItems, type, currentSensor)             } else if (type.startsWith(\"Switch\")) {                 parseSwitch(listItems, type, currentSensor)             }         }          return listItems     }      private fun parseEnvironment(listItems: ArrayList&lt;ListViewItem>, type: String, currentSensor: JSONObject)  {         var taskIcons = intArrayOf()         when (type) {             \"Environment - BMx280\" -> {                 taskIcons += R.drawable.ic_device_thermometer                 taskIcons += R.drawable.ic_device_hygrometer                 taskIcons += R.drawable.ic_device_gauge             }             \"Environment - DHT11\/12\/22  SONOFF2301\/7021\" -> {                 taskIcons += R.drawable.ic_device_thermometer                 taskIcons += R.drawable.ic_device_hygrometer             }             \"Environment - DS18b20\" -> {                 taskIcons += R.drawable.ic_device_thermometer             }         }          val taskName = currentSensor.getString(\"TaskName\")         for (taskId in taskIcons.indices) {             val currentTask = currentSensor.getJSONArray(\"TaskValues\").getJSONObject(taskId)             val currentValue = currentTask.getString(\"Value\")             if (!currentValue.equals(\"nan\")) {                 val suffix = when (taskIcons[taskId]) {                     R.drawable.ic_device_thermometer -> \" \u00b0C\"                     R.drawable.ic_device_hygrometer -> \" %\"                     R.drawable.ic_device_gauge -> \" hPa\"                     else -> \"\"                 }                 listItems += ListViewItem(                     title = currentValue + suffix,                     summary = taskName + \": \" + currentTask.getString(\"Name\"),                     icon = taskIcons[taskId]                 )             }         }     }      private fun parseSwitch(listItems: ArrayList&lt;ListViewItem>, type: String, currentSensor: JSONObject) {         when (type) {             \"Switch input - Switch\" -> {                 val currentState = currentSensor.getJSONArray(\"TaskValues\").getJSONObject(0).getInt(\"Value\") > 0                 var taskName =currentSensor.getString(\"TaskName\")                 var gpioId = \"\"                 val gpioFinder = Regex(\"~GPIO~([0-9]+)$\")                 val matchResult = gpioFinder.find(taskName)                 if (matchResult != null &amp;&amp; matchResult.groupValues.size > 1) {                     gpioId = matchResult.groupValues[1]                     taskName = taskName.replace(\"~GPIO~$gpioId\", \"\")                 }                 listItems += ListViewItem(                     title = taskName,                     summary = resources.getString(                         if (currentState) R.string.switch_summary_on                         else R.string.switch_summary_off                     ),                     hidden = gpioId,                     state = currentState,                     icon = R.drawable.ic_do                 )                 api?.needsRealTimeData = true             }         }     }      override fun parseStates(response: JSONObject): ArrayList&lt;Boolean?> {         val listItems = arrayListOf&lt;Boolean?>()          \/\/sensors         val sensors = response.optJSONArray(\"Sensors\") ?: JSONArray()         for (sensorId in 0 until sensors.length()) {             val currentSensor = sensors.getJSONObject(sensorId)             if (currentSensor.optString(\"TaskEnabled\", \"false\").equals(\"false\")) {                 continue             }              val type = currentSensor.optString(\"Type\")             if (type.startsWith(\"Environment\")) {                 parseEnvironmentStates(listItems, type, currentSensor)             } else if (type.startsWith(\"Switch\")) {                 parseSwitchStates(listItems, type, currentSensor)             }         }          return listItems     }      private fun parseEnvironmentStates(listItems: ArrayList&lt;Boolean?>, type: String, currentSensor: JSONObject)  {         var tasks = 0         when (type) {             \"Environment - BMx280\" -> tasks += 3             \"Environment - DHT11\/12\/22  SONOFF2301\/7021\" -> tasks += 2             \"Environment - DS18b20\" -> tasks++         }          for (taskId in 0 until tasks) {             if (                 !currentSensor.getJSONArray(\"TaskValues\")                     .getJSONObject(taskId)                     .getString(\"Value\")                     .equals(\"nan\")             ) listItems += null         }     }      private fun parseSwitchStates(listItems: ArrayList&lt;Boolean?>, type: String, currentSensor: JSONObject) {         when (type) {             \"Switch input - Switch\" -> {                 listItems += currentSensor.getJSONArray(\"TaskValues\").getJSONObject(0).getInt(\"Value\") > 0             }         }     } }<\/code><\/pre>\n<h2>\u0415\u0441\u043b\u0438 \u043a\u043e\u043c\u0443-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043a\u0430\u043a \u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u0441\u043f\u0435\u043a\u0442\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 kotlin, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043a\u043e\u0434\u043e\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 \u043d\u0438\u0436\u0435.<\/h2>\n<div class=\"embed_link\">\n<div class=\"embed__thumb\" style=\"background-image: url(&quot;https:\/\/opengraph.githubassets.com\/2ee6a9a14c59f85a776075fb2f4bd22c16c25fa2df27c80500328fc3c0502378\/qwoticreator\/SmartSystem&quot;);\"><\/div>\n<div class=\"embed__caption\">\n<div class=\"embed__caption-title\"><span>GitHub &#8212; qwoticreator\/SmartSystem<\/span><\/div>\n<p><a href=\"https:\/\/github.com\/qwoticreator\/SmartSystem\" target=\"_blank\" rel=\"noopener noreferrer nofollow\" class=\"embed__caption-link\">github.com<\/a><\/div>\n<\/div>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043e \u0437\u0430 \u0434\u0432\u0430 \u0434\u043d\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0445\u0430\u043a\u0430\u0442\u043e\u043d\u0430 be-coder \u0441 \u0442\u0435\u043c\u0430\u0442\u0438\u043a\u043e\u0439 \u0421\u0438\u0441\u0442\u0435\u043c\u044b \u0443\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043c\u0430. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u043a\u043e\u0434\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u043d\u0435\u0431\u0440\u0435\u0436\u043d\u043e \u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0442\u0430\u043a \u0447\u0442\u043e \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u044b\u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0441\u043e\u0432\u0435\u0442\u044b \u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u043a\u0440\u0438\u0442\u0438\u043a\u0443!)<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/post\/664312\/\"> https:\/\/habr.com\/ru\/post\/664312\/<\/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-332731","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/332731","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=332731"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/332731\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=332731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=332731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=332731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}