mm_os 3.3.1 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (380) hide show
  1. package/LICENSE +21 -201
  2. package/README.md +498 -99
  3. package/README_EN.md +505 -0
  4. package/adapter/adapter.js +431 -0
  5. package/adapter/custom_persistence.js +660 -0
  6. package/adapter/mqtt.js +273 -0
  7. package/adapter/socket.js +113 -0
  8. package/adapter/web.js +67 -0
  9. package/adapter/websocket.js +146 -0
  10. package/com/api/com.json +5 -0
  11. package/{core/com → com}/api/config.tpl.json +8 -8
  12. package/com/api/drive.js +708 -0
  13. package/com/api/index.js +198 -0
  14. package/com/api/oauth.js +200 -0
  15. package/com/api/script.tpl.js +32 -0
  16. package/com/cmd/README.md +11 -0
  17. package/com/cmd/com.json +5 -0
  18. package/com/cmd/config.tpl.json +122 -0
  19. package/com/cmd/drive.js +1548 -0
  20. package/com/cmd/index.js +1066 -0
  21. package/com/cmd/msg.json +48 -0
  22. package/com/cmd/nlp.js +525 -0
  23. package/com/cmd/script.tpl.js +32 -0
  24. package/com/db/com.json +5 -0
  25. package/com/db/drive.js +1999 -0
  26. package/com/db/index.js +242 -0
  27. package/{core/com → com}/event/README.md +4 -4
  28. package/com/event/com.json +5 -0
  29. package/{core/com → com}/event/config.tpl.json +18 -18
  30. package/com/event/drive.js +59 -0
  31. package/com/event/index.js +409 -0
  32. package/com/event/script.tpl.js +23 -0
  33. package/com/mqtt/com.json +5 -0
  34. package/{core/com → com}/mqtt/config.tpl.json +3 -5
  35. package/com/mqtt/drive.js +676 -0
  36. package/com/mqtt/index.js +822 -0
  37. package/com/mqtt/mm_mqtt.js +425 -0
  38. package/com/mqtt/script.tpl.js +723 -0
  39. package/com/nav/com.json +5 -0
  40. package/com/nav/config.tpl.json +84 -0
  41. package/com/nav/drive.js +702 -0
  42. package/com/nav/index.js +231 -0
  43. package/{core/com → com}/nav/tpl/admin_pc/page_config.vue +280 -280
  44. package/{core/com → com}/nav/tpl/admin_pc/page_config_form.vue +194 -194
  45. package/com/nav/tpl/admin_pc/page_form.vue +180 -0
  46. package/com/nav/tpl/admin_pc/page_view.vue +124 -0
  47. package/com/nav/tpl/dev_pc/page_default.vue +247 -0
  48. package/com/nav/tpl/dev_pc/page_type.vue +313 -0
  49. package/com/nav/tpl/home_pc/page_default.vue +234 -0
  50. package/com/nav/tpl/home_pc/page_form.vue +137 -0
  51. package/com/nav/tpl/home_pc/page_list.vue +234 -0
  52. package/com/nav/tpl/home_pc/page_nav.vue +221 -0
  53. package/com/nav/tpl/home_pc/page_type.vue +234 -0
  54. package/com/nav/tpl/home_pc/page_view.vue +125 -0
  55. package/com/nav/tpl/home_phone/page_channel.vue +234 -0
  56. package/com/nav/tpl/home_phone/page_default.vue +234 -0
  57. package/com/nav/tpl/home_phone/page_form.vue +137 -0
  58. package/com/nav/tpl/home_phone/page_nav.vue +237 -0
  59. package/com/nav/tpl/home_phone/page_type.vue +234 -0
  60. package/com/nav/tpl/home_phone/page_view.vue +125 -0
  61. package/com/nav/viewmodel.js +446 -0
  62. package/com/param/com.json +5 -0
  63. package/{core/com → com}/param/config.tpl.json +7 -1
  64. package/com/param/drive.js +502 -0
  65. package/com/param/index.js +155 -0
  66. package/com/param/script.tpl.js +12 -0
  67. package/com/pendant/com.json +5 -0
  68. package/{core/com/component → com/pendant}/config.tpl.json +15 -13
  69. package/com/pendant/drive.js +204 -0
  70. package/com/pendant/index.js +441 -0
  71. package/com/pendant/pendant.html +16 -0
  72. package/com/pendant/script.tpl.js +18 -0
  73. package/com/socket/com.json +5 -0
  74. package/com/socket/config.tpl.json +12 -0
  75. package/com/socket/drive.js +651 -0
  76. package/com/socket/index.js +351 -0
  77. package/com/socket/script.tpl.js +41 -0
  78. package/com/sql/com.json +5 -0
  79. package/{core/com → com}/sql/config.tpl.json +13 -9
  80. package/com/sql/drive.js +1259 -0
  81. package/com/sql/index.js +150 -0
  82. package/com/sql/script.tpl.js +47 -0
  83. package/com/static/com.json +5 -0
  84. package/{core/com → com}/static/config.tpl.json +10 -6
  85. package/com/static/drive.js +194 -0
  86. package/com/static/index.js +226 -0
  87. package/com/static/script.tpl.js +28 -0
  88. package/com/task/com.json +5 -0
  89. package/{core/com → com}/task/config.tpl.json +4 -6
  90. package/com/task/drive.js +405 -0
  91. package/com/task/index.js +148 -0
  92. package/com/task/script.tpl.js +37 -0
  93. package/com/template/com.json +5 -0
  94. package/com/template/config.tpl.json +16 -0
  95. package/com/template/drive.js +80 -0
  96. package/com/template/index.js +141 -0
  97. package/com.js +156 -0
  98. package/common/README.md +2 -0
  99. package/common/handler/msg/handler.json +22 -0
  100. package/common/handler/msg/index.js +23 -0
  101. package/common/handler/player/handler.json +22 -0
  102. package/common/handler/player/index.js +287 -0
  103. package/common/handler/user/handler.json +22 -0
  104. package/common/handler/user/index.js +23 -0
  105. package/common/middleware/web_after/index.js +29 -0
  106. package/common/middleware/web_after/middleware.json +9 -0
  107. package/common/middleware/web_base/index.js +113 -0
  108. package/common/middleware/web_base/middleware.json +19 -0
  109. package/common/middleware/web_before/index.js +33 -0
  110. package/common/middleware/web_before/middleware.json +9 -0
  111. package/common/middleware/web_cors/index.js +87 -0
  112. package/common/middleware/web_cors/middleware.json +24 -0
  113. package/common/middleware/web_error/index.js +119 -0
  114. package/common/middleware/web_error/middleware.json +18 -0
  115. package/common/middleware/web_ip/index.js +15 -0
  116. package/common/middleware/web_ip/middleware.json +14 -0
  117. package/common/middleware/web_logger/index.js +156 -0
  118. package/common/middleware/web_logger/middleware.json +14 -0
  119. package/common/middleware/web_main/index.js +24 -0
  120. package/common/middleware/web_main/middleware.json +9 -0
  121. package/common/middleware/web_static/index.js +73 -0
  122. package/common/middleware/web_static/middleware.json +54 -0
  123. package/common/middleware/web_waf/index.js +385 -0
  124. package/common/middleware/web_waf/middleware.json +13 -0
  125. package/common/model/msg/index.js +88 -0
  126. package/common/model/msg/model.json +401 -0
  127. package/common/model/player/index.js +63 -0
  128. package/common/model/player/model.json +185 -0
  129. package/common/model/user/index.js +11 -0
  130. package/common/model/user/model.json +219 -0
  131. package/core/app/config.tpl.json +67 -0
  132. package/core/app/index.js +632 -0
  133. package/core/app/script.tpl.js +52 -0
  134. package/core/channel/index.js +899 -0
  135. package/core/channel/matcher.js +585 -0
  136. package/core/com/config.tpl.json +16 -0
  137. package/core/com/index.js +74 -0
  138. package/core/com/script.tpl.js +5 -0
  139. package/core/component/component.js +42 -0
  140. package/core/component/config.tpl.json +63 -0
  141. package/core/component/index.js +273 -0
  142. package/core/component/script.tpl.js +19 -0
  143. package/core/controller/config.tpl.json +14 -0
  144. package/core/controller/index.js +373 -0
  145. package/core/controller/script.tpl.js +27 -0
  146. package/core/factory/config.tpl.json +14 -0
  147. package/core/factory/entity.js +275 -0
  148. package/core/factory/index.js +241 -0
  149. package/core/factory/script.tpl.js +16 -0
  150. package/core/game/bat/index.js +137 -0
  151. package/core/game/bat/world.js +622 -0
  152. package/core/game/config.tpl.json +16 -0
  153. package/core/game/entity_admin.js +230 -0
  154. package/core/game/index.js +186 -0
  155. package/core/handler/config.tpl.json +22 -0
  156. package/core/handler/index.js +181 -0
  157. package/core/handler/script.tpl.js +23 -0
  158. package/core/logic/config.tpl.json +14 -0
  159. package/core/logic/index.js +59 -0
  160. package/core/logic/script.tpl.js +19 -0
  161. package/core/middleware/config.tpl.json +16 -0
  162. package/core/middleware/index.js +125 -0
  163. package/core/middleware/script.tpl.js +37 -0
  164. package/core/mod/config.tpl.json +22 -0
  165. package/core/mod/index.js +130 -0
  166. package/core/mod/script.tpl.js +34 -0
  167. package/core/model/config.tpl.json +219 -0
  168. package/core/model/index.js +272 -0
  169. package/core/model/model.js +27 -0
  170. package/core/model/script.tpl.js +20 -0
  171. package/core/notifier/config.tpl.json +14 -0
  172. package/core/notifier/index.js +77 -0
  173. package/core/notifier/script.tpl.js +20 -0
  174. package/core/plugin/config.tpl.json +24 -0
  175. package/core/plugin/index.js +232 -0
  176. package/core/plugin/script.tpl.js +51 -0
  177. package/core/pusher/config.tpl.json +14 -0
  178. package/core/pusher/index.js +161 -0
  179. package/core/pusher/script.tpl.js +20 -0
  180. package/core/room/bat/index.js +170 -0
  181. package/core/room/bat/room.js +524 -0
  182. package/core/room/config.tpl.json +20 -0
  183. package/core/room/index.js +249 -0
  184. package/core/room/room.js +61 -0
  185. package/core/scene/config.tpl.json +14 -0
  186. package/core/scene/index.js +466 -0
  187. package/core/scene/loop.js +1255 -0
  188. package/core/scene/map.js +28 -0
  189. package/core/scene/script.tpl.js +22 -0
  190. package/core/sender/config.tpl.json +14 -0
  191. package/core/sender/index.js +79 -0
  192. package/core/sender/script.tpl.js +20 -0
  193. package/core/service/config.tpl.json +14 -0
  194. package/core/service/index.js +100 -0
  195. package/core/service/script.tpl.js +25 -0
  196. package/core/store/config.tpl.json +26 -0
  197. package/core/store/index.js +1755 -0
  198. package/core/store/script.tpl.js +22 -0
  199. package/core/store/sql.js +1464 -0
  200. package/core/system/config.tpl.json +18 -0
  201. package/core/system/index.js +312 -0
  202. package/core/system/script.tpl.js +77 -0
  203. package/core/view/config.tpl.json +14 -0
  204. package/core/view/index.js +91 -0
  205. package/core/view/script.tpl.js +20 -0
  206. package/core/zone/bat/index.js +725 -0
  207. package/core/zone/config.tpl.json +54 -0
  208. package/core/zone/index.js +614 -0
  209. package/core/zone/script.tpl.js +10 -0
  210. package/core/zone/zone_bat.js +136 -0
  211. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +24 -0
  212. package/index.js +17 -333
  213. package/os.js +57 -0
  214. package/package.json +65 -55
  215. package/server.js +598 -0
  216. package/README.en.md +0 -36
  217. package/conf.json +0 -3
  218. package/core/base/mqtt/index.js +0 -1110
  219. package/core/base/mqtt/lib.js +0 -40
  220. package/core/base/web/index.js +0 -245
  221. package/core/com/api/com.json +0 -4
  222. package/core/com/api/drive.js +0 -668
  223. package/core/com/api/index.js +0 -108
  224. package/core/com/api/oauth.js +0 -158
  225. package/core/com/api/script.js +0 -32
  226. package/core/com/app/README.md +0 -3
  227. package/core/com/app/com.json +0 -4
  228. package/core/com/app/config.tpl.json +0 -16
  229. package/core/com/app/drive.js +0 -309
  230. package/core/com/app/index.js +0 -211
  231. package/core/com/app/script.js +0 -155
  232. package/core/com/cmd/com.json +0 -4
  233. package/core/com/cmd/config.tpl.json +0 -66
  234. package/core/com/cmd/drive.js +0 -513
  235. package/core/com/cmd/index.js +0 -354
  236. package/core/com/cmd/old/5w2h.js +0 -54
  237. package/core/com/cmd/old/drive.js +0 -423
  238. package/core/com/cmd/script.js +0 -11
  239. package/core/com/component/README.md +0 -3
  240. package/core/com/component/com.json +0 -4
  241. package/core/com/component/component.html +0 -16
  242. package/core/com/component/drive.js +0 -197
  243. package/core/com/component/index.js +0 -312
  244. package/core/com/component/script.js +0 -18
  245. package/core/com/db/com.json +0 -4
  246. package/core/com/db/drive.js +0 -1160
  247. package/core/com/db/index.js +0 -176
  248. package/core/com/event/com.json +0 -4
  249. package/core/com/event/drive.js +0 -133
  250. package/core/com/event/index.js +0 -345
  251. package/core/com/event/script.js +0 -26
  252. package/core/com/eventer/com.js +0 -477
  253. package/core/com/eventer/com.json +0 -4
  254. package/core/com/middleware/com.js +0 -154
  255. package/core/com/middleware/com.json +0 -4
  256. package/core/com/middleware/config.tpl.json +0 -8
  257. package/core/com/middleware/script.js +0 -9
  258. package/core/com/mqtt/com.json +0 -4
  259. package/core/com/mqtt/drive.js +0 -600
  260. package/core/com/mqtt/index.js +0 -572
  261. package/core/com/mqtt/mm_mqtt.js +0 -330
  262. package/core/com/mqtt/script.js +0 -604
  263. package/core/com/msg/com.js +0 -296
  264. package/core/com/msg/com.json +0 -4
  265. package/core/com/nav/com.json +0 -4
  266. package/core/com/nav/config.tpl.json +0 -75
  267. package/core/com/nav/drive.js +0 -549
  268. package/core/com/nav/index.js +0 -182
  269. package/core/com/nav/tpl/admin_pc/page_form.vue +0 -180
  270. package/core/com/nav/tpl/admin_pc/page_view.vue +0 -124
  271. package/core/com/nav/tpl/dev_pc/page_default.vue +0 -247
  272. package/core/com/nav/tpl/dev_pc/page_type.vue +0 -313
  273. package/core/com/nav/tpl/home_pc/page_default.vue +0 -234
  274. package/core/com/nav/tpl/home_pc/page_form.vue +0 -137
  275. package/core/com/nav/tpl/home_pc/page_list.vue +0 -234
  276. package/core/com/nav/tpl/home_pc/page_nav.vue +0 -221
  277. package/core/com/nav/tpl/home_pc/page_type.vue +0 -234
  278. package/core/com/nav/tpl/home_pc/page_view.vue +0 -125
  279. package/core/com/nav/tpl/home_phone/page_channel.vue +0 -234
  280. package/core/com/nav/tpl/home_phone/page_default.vue +0 -234
  281. package/core/com/nav/tpl/home_phone/page_form.vue +0 -137
  282. package/core/com/nav/tpl/home_phone/page_nav.vue +0 -237
  283. package/core/com/nav/tpl/home_phone/page_type.vue +0 -234
  284. package/core/com/nav/tpl/home_phone/page_view.vue +0 -125
  285. package/core/com/nav/viewmodel.js +0 -296
  286. package/core/com/param/drive.js +0 -366
  287. package/core/com/param/index.js +0 -80
  288. package/core/com/param/script.js +0 -12
  289. package/core/com/param/test.js +0 -98
  290. package/core/com/plugin/README.md +0 -3
  291. package/core/com/plugin/com.json +0 -4
  292. package/core/com/plugin/config.tpl.json +0 -26
  293. package/core/com/plugin/drive.js +0 -536
  294. package/core/com/plugin/index.js +0 -259
  295. package/core/com/plugin/script.js +0 -213
  296. package/core/com/rpc/com.json +0 -4
  297. package/core/com/rpc/drive.js +0 -160
  298. package/core/com/rpc/index.js +0 -87
  299. package/core/com/rpc/rpc.js +0 -118
  300. package/core/com/socket/com.json +0 -4
  301. package/core/com/socket/config.tpl.json +0 -14
  302. package/core/com/socket/drive.js +0 -403
  303. package/core/com/socket/index.js +0 -62
  304. package/core/com/socket/script.js +0 -42
  305. package/core/com/sql/drive.js +0 -1087
  306. package/core/com/sql/index.js +0 -83
  307. package/core/com/sql/script.js +0 -48
  308. package/core/com/static/com.json +0 -4
  309. package/core/com/static/drive.js +0 -220
  310. package/core/com/static/index.js +0 -149
  311. package/core/com/static/script.js +0 -28
  312. package/core/com/task/com.json +0 -4
  313. package/core/com/task/drive.js +0 -403
  314. package/core/com/task/index.js +0 -110
  315. package/core/com/task/script.js +0 -37
  316. package/core/com/timer/com.js +0 -217
  317. package/core/com/timer/com.json +0 -4
  318. package/core/com/tpl/com.js +0 -19
  319. package/core/com/tpl/com.json +0 -4
  320. package/lib/actions.js +0 -50
  321. package/lib/base.js +0 -361
  322. package/lib/com.js +0 -29
  323. package/lib/ref.js +0 -121
  324. package/middleware/cors/index.js +0 -119
  325. package/middleware/cors/middleware.json +0 -20
  326. package/middleware/csrf/index.js +0 -202
  327. package/middleware/csrf/middleware.json +0 -24
  328. package/middleware/ip_firewall/index.js +0 -476
  329. package/middleware/ip_firewall/middleware.json +0 -109
  330. package/middleware/mqtt_base/index.js +0 -10
  331. package/middleware/mqtt_base/middleware.json +0 -11
  332. package/middleware/security_audit/index.js +0 -543
  333. package/middleware/security_audit/middleware.json +0 -48
  334. package/middleware/waf/index.js +0 -343
  335. package/middleware/waf/middleware.json +0 -10
  336. package/middleware/waf_ddos/index.js +0 -520
  337. package/middleware/waf_ddos/middleware.json +0 -38
  338. package/middleware/waf_xss/index.js +0 -269
  339. package/middleware/waf_xss/middleware.json +0 -18
  340. package/middleware/web_after/index.js +0 -33
  341. package/middleware/web_after/middleware.json +0 -10
  342. package/middleware/web_base/index.js +0 -90
  343. package/middleware/web_base/middleware.json +0 -10
  344. package/middleware/web_before/index.js +0 -27
  345. package/middleware/web_before/middleware.json +0 -10
  346. package/middleware/web_check/index.js +0 -28
  347. package/middleware/web_check/middleware.json +0 -10
  348. package/middleware/web_main/index.js +0 -28
  349. package/middleware/web_main/middleware.json +0 -10
  350. package/middleware/web_proxy/index.js +0 -37
  351. package/middleware/web_proxy/middleware.json +0 -10
  352. package/middleware/web_render/index.js +0 -87
  353. package/middleware/web_render/middleware.json +0 -10
  354. package/middleware/web_socket/index.js +0 -34
  355. package/middleware/web_socket/middleware.json +0 -10
  356. package/middleware/web_static/index.js +0 -115
  357. package/middleware/web_static/middleware.json +0 -10
  358. /package/{core/com → com}/api/README.md +0 -0
  359. /package/{core/com → com}/db/README.md +0 -0
  360. /package/{core/com → com}/mqtt/README.md +0 -0
  361. /package/{core/com → com}/nav/README.md +0 -0
  362. /package/{core/com → com}/nav/tpl/admin_pc/page_channel.vue +0 -0
  363. /package/{core/com → com}/nav/tpl/admin_pc/page_default.vue +0 -0
  364. /package/{core/com → com}/nav/tpl/admin_pc/page_lang.vue +0 -0
  365. /package/{core/com → com}/nav/tpl/admin_pc/page_nav.vue +0 -0
  366. /package/{core/com → com}/nav/tpl/admin_pc/page_table.vue +0 -0
  367. /package/{core/com → com}/nav/tpl/admin_pc/page_type.vue +0 -0
  368. /package/{core/com → com}/nav/tpl/dev_pc/page_channel.vue +0 -0
  369. /package/{core/com → com}/nav/tpl/dev_pc/page_config.vue +0 -0
  370. /package/{core/com → com}/nav/tpl/dev_pc/page_form.vue +0 -0
  371. /package/{core/com → com}/nav/tpl/dev_pc/page_nav.vue +0 -0
  372. /package/{core/com → com}/nav/tpl/dev_pc/page_table.vue +0 -0
  373. /package/{core/com → com}/nav/tpl/home_pc/page_channel.vue +0 -0
  374. /package/{core/com → com}/nav/tpl/home_phone/page_list.vue +0 -0
  375. /package/{core/com → com}/param/README.md +0 -0
  376. /package/{core/com/cmd → com/pendant}/README.md +0 -0
  377. /package/{core/com → com}/socket/README.md +0 -0
  378. /package/{core/com → com}/sql/README.md +0 -0
  379. /package/{core/com → com}/static/README.md +0 -0
  380. /package/{core/com → com}/task/README.md +0 -0
@@ -0,0 +1,822 @@
1
+ const Mqtt_client = require('mqtt');
2
+ const Manager = require('mm_machine').Manager;
3
+ const Drive = require('./drive');
4
+
5
+ /**
6
+ * Mqtt通讯类
7
+ * @augments {Manager}
8
+ * @class
9
+ */
10
+ class Mqtt extends Manager {
11
+ /**
12
+ * 配置参数
13
+ * @type {object}
14
+ */
15
+ static config = {
16
+ /**
17
+ * 名称
18
+ * @type {string}
19
+ */
20
+ name: '',
21
+ /**
22
+ * 标题
23
+ * @type {string}
24
+ */
25
+ title: 'Mqtt通讯类',
26
+ /**
27
+ * 描述
28
+ * @type {string}
29
+ */
30
+ description: '这是Mqtt通讯类管理器',
31
+ /**
32
+ * 检索文件名
33
+ * @type {string}
34
+ */
35
+ filename: 'mqtt.json',
36
+ /**
37
+ * 模板目录
38
+ * @type {string}
39
+ */
40
+ tpl_dir: __dirname,
41
+ /**
42
+ * 基础目录
43
+ * @type {string}
44
+ */
45
+ base_dir: '../common/mqtt'.fullname(__dirname),
46
+ /**
47
+ * 自定义目录,加载项目自定义资源
48
+ * @type {string}
49
+ */
50
+ dir: './app'.fullname(),
51
+ /**
52
+ * 搜索模式 dir按目录搜索 | file按文件名搜索
53
+ * @type {string}
54
+ */
55
+ search_way: 'file',
56
+ /**
57
+ * 是否懒加载
58
+ * @type {boolean}
59
+ */
60
+ lazy_load: true,
61
+ /**
62
+ * 模式
63
+ * 1.生产模式,改变文件不会重新加载
64
+ * 2.热更新模式,改变配置文件会重新加载配置,不重新加载脚本
65
+ * 3.热重载模式,改变配置文件都会加载配置和脚本
66
+ * 4.重载模式,执行完后重新加载脚本,避免变量污染
67
+ * 5.热更新+重载模式,改变配置文件重新加载配置和脚本,执行完后重新加载脚本
68
+ * @type {number}
69
+ */
70
+ mode: 3,
71
+ hostname: '127.0.0.1',
72
+ port: '1883',
73
+ protocol: 'mqtt',
74
+ client_id: 'server',
75
+ subscribe_qos: 2,
76
+ publish_qos: 2,
77
+ username: 'server',
78
+ password: 'asd123',
79
+ table: 'iot_device',
80
+ clean: false,
81
+ longtime: 15000,
82
+ request_timeout: 3000,
83
+ // 在线有效期时长判断 单位:毫秒,150000毫秒为2.5分钟
84
+ online_expires: 150000,
85
+ // 在线有效期检测间隔
86
+ online_interval: 10000,
87
+ // 连接超时
88
+ connect_timeout: 30000,
89
+ // maxInflight: 20,
90
+ // 重连间隔
91
+ reco_period: 6000 // 重连间隔1秒
92
+ };
93
+
94
+ /**
95
+ * 构造函数
96
+ * @param {object} config 配置参数
97
+ * @param {object} parent 父级模块
98
+ */
99
+ constructor(config, parent) {
100
+ super({ ...Mqtt.config, ...config }, parent);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * 预置
106
+ */
107
+ Mqtt.prototype._preset = function () {
108
+ this.dict = {};
109
+ // 定时器
110
+ this.timer = null;
111
+ // mqtt客户端服务器
112
+ this.client = null;
113
+ /**
114
+ * message queue
115
+ */
116
+ this.list_msg = [
117
+ /*
118
+ {
119
+ // information ID
120
+ id: "",
121
+ // request method
122
+ method: "",
123
+ // message parameters
124
+ params: {},
125
+ // Callback
126
+ func: function(res){}
127
+ }
128
+ */
129
+ ];
130
+
131
+ /**
132
+ * 订阅集合 (主题 => 函数列表)
133
+ */
134
+ this.dict_subscribe = {};
135
+
136
+ /**
137
+ * method collection
138
+ */
139
+ var _this = this;
140
+ this.methods = {
141
+ /**
142
+ * 获取所有方法
143
+ * @returns {Array} 函数列表
144
+ */
145
+ getMethod: function () {
146
+ return Object.keys(_this.methods);
147
+ }
148
+ };
149
+
150
+ this.retry_times = 0;
151
+ this.connecting = false;
152
+ this._createClient();
153
+ }
154
+
155
+ /**
156
+ * mqtt驱动类
157
+ * @type {Drive}
158
+ */
159
+ Mqtt.prototype.Drive = Drive;
160
+
161
+ /**
162
+ * 主题匹配
163
+ * @param {string} topic 接收到的主题
164
+ * @param {string} top 匹配用的主题
165
+ * @returns {boolean} 返回匹配结果,正确返回true,错误返回false
166
+ */
167
+ Mqtt.prototype.match = function (topic, top) {
168
+ if (topic === top) {
169
+ return true;
170
+ }
171
+ var str = '^' + top.replace('#', '.*').replace('+', '~~~').replace('~~~', '[a-zA-Z0-9_-]+').replace('$', '~~~')
172
+ .replace('~~~', '\\$');
173
+ var s = str.substring(str.length - 1, str.length);
174
+ if (s !== '#') {
175
+ str += '$';
176
+ }
177
+ var mh = new RegExp(str);
178
+ return mh.test(topic);
179
+ };
180
+
181
+ /**
182
+ * 初始化
183
+ */
184
+ Mqtt.prototype.updateAfter = async function () {
185
+ var _this = this;
186
+ let infos = this.getInfos();
187
+ for (var i = 0; i < infos.length; i++) {
188
+ var info = infos[i];
189
+ if (info.state === 1) {
190
+ let mod = this.getMod(info.name);
191
+ if (mod) {
192
+ mod.send = function (topic, msg, qos = 0, retain = false) {
193
+ if (topic) {
194
+ _this.send(topic, msg, qos, retain);
195
+ }
196
+ };
197
+ await mod.do('init');
198
+ var topics = mod.config.topic;
199
+ if (topics) {
200
+ for (var n = 0; n < topics.length; n++) {
201
+ var tc = topics[n];
202
+ var retain = mod.config.retain || false;
203
+ var qos = mod.config.qos || 0;
204
+ if (tc.indexOf('heartbeat') !== -1) {
205
+ retain = false;
206
+ qos = 0;
207
+ }
208
+ this.subscribe(tc, null, qos, retain);
209
+ }
210
+ }
211
+ }
212
+ }
213
+ }
214
+ };
215
+
216
+ /**
217
+ * 重新连接
218
+ */
219
+ Mqtt.prototype.reconnect = function () {
220
+ // $.sleep(this.config.interval);
221
+ // this.retryTimes += 1;
222
+ // if (this.retryTimes > 5) {
223
+ // try {
224
+ // this.client.end();
225
+ // this.init();
226
+ // } catch (error) {
227
+ // this.$message.error(error.toString());
228
+ // }
229
+ // }
230
+ };
231
+
232
+ /**
233
+ * 初始化
234
+ * @param {object} config 配置参数
235
+ */
236
+ Mqtt.prototype.init = function (config) {
237
+ this.config = Object.assign(this.config, config);
238
+ this.retry_times = 0;
239
+ this.connecting = false;
240
+ };
241
+
242
+ /**
243
+ * 运行Mqtt
244
+ * @returns {Promise} 连接结果
245
+ */
246
+ Mqtt.prototype.start = function () {
247
+ setTimeout(() => {
248
+ this.startTimer();
249
+ }, 2000);
250
+
251
+ this.connecting = true;
252
+
253
+ try {
254
+ this._createClient();
255
+
256
+ if (this.client && this.client.on) {
257
+ return this._setupClientEvents();
258
+ }
259
+ } catch (error) {
260
+ this.connecting = false;
261
+ return this._rejectPromise(error);
262
+ }
263
+
264
+ return this._rejectPromise();
265
+ };
266
+
267
+ /**
268
+ * 创建客户端
269
+ * @private
270
+ */
271
+ Mqtt.prototype._createClient = function () {
272
+ var cg = { ...this.config };
273
+ cg.clientId = cg.client_id;
274
+ if (cg.host) {
275
+ delete cg.hostname;
276
+ delete cg.port;
277
+ this.client = Mqtt_client.connect(cg.host, cg);
278
+ } else {
279
+ this.client = Mqtt_client.connect(cg);
280
+ }
281
+ };
282
+
283
+ /**
284
+ * 设置客户端事件
285
+ * @returns {Promise} 连接结果
286
+ * @private
287
+ */
288
+ Mqtt.prototype._setupClientEvents = function () {
289
+ return new Promise((resolve, reject) => {
290
+ this.client.on('connect', (packet, err) => {
291
+ this.connecting = false;
292
+ if (err) {
293
+ resolve(null);
294
+ reject(err);
295
+ } else {
296
+ this.client.on('message', async (topic, message) => {
297
+ await this.receive(topic, message.toString());
298
+ });
299
+ resolve(packet);
300
+ }
301
+ });
302
+ this.client.on('reconnect', () => {
303
+ this.reconnect();
304
+ });
305
+ this.client.on('error', (error) => {
306
+ console.error('Connection failed', error);
307
+ });
308
+ });
309
+ };
310
+
311
+ /**
312
+ * 创建被拒绝的Promise
313
+ * @param {Error} error 错误对象
314
+ * @returns {Promise} 被拒绝的Promise
315
+ * @private
316
+ */
317
+ Mqtt.prototype._rejectPromise = function (error) {
318
+ return new Promise((resolve, reject) => {
319
+ resolve(null);
320
+ reject(error);
321
+ });
322
+ };
323
+
324
+ /**
325
+ * 监听事件
326
+ * @param {string} topic 事件名称
327
+ * @param {Function} func 回调函数
328
+ */
329
+ Mqtt.prototype.on = function (topic, func) {
330
+ this.client.on(topic, func);
331
+ };
332
+
333
+ /**
334
+ * 接收消息
335
+ * @param {string} topic 订阅板块
336
+ * @param {object} msg 消息主体
337
+ */
338
+ Mqtt.prototype.message = async function (topic, msg) {
339
+ if (this.dict_subscribe[topic]) {
340
+ var list = this.dict_subscribe[topic];
341
+ try {
342
+ for (var key in list) {
343
+ list[key](msg);
344
+ }
345
+ } catch (err) {
346
+ console.error(err);
347
+ }
348
+ }
349
+ };
350
+
351
+ /**
352
+ * 接收消息
353
+ * @param {string} push_topic 推送过来的主题
354
+ * @param {object} message 消息主体
355
+ * @returns {object} 解析后的消息主体
356
+ */
357
+ Mqtt.prototype.receive = async function (push_topic, message) {
358
+ var msg = this._parseMessage(message, push_topic);
359
+ if (msg === null) {
360
+ return;
361
+ }
362
+
363
+ // 处理消息回复
364
+ this._handleResponse(msg);
365
+
366
+ var ret = await this._processTopics(push_topic, msg);
367
+ try {
368
+ await this.message(ret.topic, msg);
369
+ } catch (error) {
370
+ this.log('error', 'mqtt处理程序错误', error);
371
+ return;
372
+ }
373
+ return ret.result;
374
+ };
375
+
376
+ /**
377
+ * 解析消息
378
+ * @param {object} message 消息主体
379
+ * @param {string} push_topic 推送过来的主题
380
+ * @returns {object|null} 解析后的消息
381
+ * @private
382
+ */
383
+ Mqtt.prototype._parseMessage = function (message, push_topic) {
384
+ var msg = {};
385
+ if (message && (message.indexOf('[') === 0 || message.indexOf('{') === 0)) {
386
+ try {
387
+ msg = JSON.parse(message);
388
+ } catch (error) {
389
+ this.log('error', '消息结构体不对', push_topic, error);
390
+ return null;
391
+ }
392
+ }
393
+ return msg;
394
+ };
395
+
396
+ /**
397
+ * 处理主题
398
+ * @param {string} push_topic 推送过来的主题
399
+ * @param {object} msg 消息主体
400
+ * @returns {object} 处理结果和主题
401
+ * @private
402
+ */
403
+ Mqtt.prototype._processTopics = async function (push_topic, msg) {
404
+ var ret;
405
+ var topic;
406
+ var mods = this.getMods();
407
+ for (var k in mods) {
408
+ var o = mods[k];
409
+ if (o.config.state !== 1) {
410
+ continue;
411
+ }
412
+ var topics = o.config.topic;
413
+ if (!topics) {
414
+ continue;
415
+ }
416
+ for (var n = 0; n < topics.length; n++) {
417
+ topic = topics[n];
418
+ if (this.match(push_topic, topic)) {
419
+ ret = await o.call('handle', push_topic, msg, topic, n);
420
+ }
421
+ }
422
+ }
423
+ return { result: ret, topic };
424
+ };
425
+
426
+ /**
427
+ * 订阅主题
428
+ * @param {string} topic 主题
429
+ * @param {Function} func 回调函数
430
+ * @param {number} qos 推送方式 0
431
+ * @param {boolean} retain 保持
432
+ * @returns {string} 主键
433
+ */
434
+ Mqtt.prototype.subscribe = function (topic, func, qos = null, retain = false) {
435
+ if (!this.client) {
436
+ return '';
437
+ }
438
+ this.client.subscribe(topic, {
439
+ // 订阅消息方式,0为保留 , 1为确认收到1次
440
+ qos: qos || this.config.subscribe_qos || 0,
441
+ retain
442
+ });
443
+ if (func) {
444
+ if (!this.dict_subscribe[topic]) {
445
+ this.dict_subscribe[topic] = {};
446
+ }
447
+ var key = this.key_num + 1;
448
+ this.dict_subscribe[topic][key] = func;
449
+ return key;
450
+ }
451
+ return '';
452
+ };
453
+
454
+ /**
455
+ * 取消订阅
456
+ * @param {string} topic 主题
457
+ * @param {string} key 主键
458
+ */
459
+ Mqtt.prototype.unsubscribe = function (topic, key) {
460
+ this.client.unsubscribe(topic);
461
+ if (this.dict_subscribe[topic]) {
462
+ delete this.dict_subscribe[topic][key];
463
+ }
464
+ };
465
+
466
+ /**
467
+ * 结束客户端
468
+ */
469
+ Mqtt.prototype.end = function () {
470
+ if (this.client) {
471
+ this.client.end();
472
+ }
473
+ if (this.timer) {
474
+ clearInterval(this.timer);
475
+ this.timer = null;
476
+ }
477
+ };
478
+
479
+ /**
480
+ * 发布主题
481
+ * @param {string} topic 主题
482
+ * @param {object} message 消息对象
483
+ * @param {number} qos 推送方式 0
484
+ * @param {boolean} retain 保持
485
+ */
486
+ Mqtt.prototype.publish = function (topic, message, qos = null, retain = false) {
487
+ this.client.publish(topic, message, {
488
+ qos: qos || this.config.publish_qos || 0,
489
+ retain
490
+ });
491
+ };
492
+
493
+ /**
494
+ * 主题推送
495
+ * @param {string} topic 主题
496
+ * @param {string} msg_obj 消息主体
497
+ * @param {number} qos 推送方式 0
498
+ * @param {boolean} retain 保持
499
+ */
500
+ Mqtt.prototype.send = function (topic, msg_obj, qos = null, retain = false) {
501
+ this.publish(topic, JSON.stringify(msg_obj), qos, retain);
502
+ };
503
+
504
+ /**
505
+ * 生成ID
506
+ * @param {string} clientId 客户端ID
507
+ * @returns {string} ID
508
+ */
509
+ Mqtt.prototype.genId = function (clientId) {
510
+ return clientId + '_' + Date.parse(new Date());
511
+ };
512
+
513
+ /**
514
+ * 请求
515
+ * @param {string} topic 主题
516
+ * @param {string} method 请求方法
517
+ * @param {object} params 请求参数
518
+ * @param {Function} func 回调函数
519
+ * @param {number} timeout 超时时间(毫秒)
520
+ */
521
+ Mqtt.prototype.req = function (topic, method, params, func, timeout = 0) {
522
+ var data = {
523
+ id: this.genId(this.client.options.clientId),
524
+ method,
525
+ params
526
+ };
527
+
528
+ if (func) {
529
+ data.func = func;
530
+ data.timestamp = Date.now();
531
+ data.timeout = timeout || this.config.request_timeout || 3000;
532
+ this.list_msg.push(data);
533
+
534
+ // 设置超时移除定时器
535
+ var _this = this;
536
+ data.timer = setTimeout(function () {
537
+ _this._delMsgById(data.id);
538
+ }, data.timeout);
539
+ }
540
+ this.send(topic, data);
541
+ };
542
+
543
+ /**
544
+ * 同步请求 - 可及时取回消息
545
+ * @param {string} topic 订阅板块
546
+ * @param {string} method 请求方法
547
+ * @param {object} params 传递参数
548
+ * @param {number} timeout 超时时间 0为默认值 10000ms
549
+ * @returns {object} 返回响应结果
550
+ */
551
+ Mqtt.prototype.reqAsync = function (topic, method, params, timeout = 0) {
552
+ var _this = this;
553
+ return new Promise((resolve, reject) => {
554
+ _this.req(topic, method, params, (res) => {
555
+ resolve(res);
556
+ }, timeout);
557
+ });
558
+ };
559
+
560
+ /**
561
+ * 根据ID移除消息
562
+ * @param {string} msg_id 消息ID
563
+ * @private
564
+ */
565
+ Mqtt.prototype._delMsgById = function (msg_id) {
566
+ for (var i = 0; i < this.list_msg.length; i++) {
567
+ if (this.list_msg[i].id === msg_id) {
568
+ // 清除定时器
569
+ if (this.list_msg[i].timer) {
570
+ clearTimeout(this.list_msg[i].timer);
571
+ }
572
+ this.list_msg.splice(i, 1);
573
+ break;
574
+ }
575
+ }
576
+ };
577
+
578
+ /**
579
+ * 处理消息回复
580
+ * @param {object} msg 回复消息
581
+ * @private
582
+ */
583
+ Mqtt.prototype._handleResponse = function (msg) {
584
+ if (!msg || !msg.id) {
585
+ return;
586
+ }
587
+
588
+ for (var i = 0; i < this.list_msg.length; i++) {
589
+ var msg_item = this.list_msg[i];
590
+ if (msg_item.id === msg.id && msg_item.func) {
591
+ try {
592
+ // 执行回调函数
593
+ msg_item.func(msg);
594
+ } catch (error) {
595
+ console.error('MQTT回调函数执行错误:', error);
596
+ }
597
+
598
+ // 从列表中移除已处理的消息
599
+ this._delMsgById(msg.id);
600
+ break;
601
+ }
602
+ }
603
+ };
604
+
605
+ /**
606
+ * 保存在线到数据库
607
+ * @param {Array} arr 客户端ID数组
608
+ * @param {number} online 在线情况 0为不在线,1为在线
609
+ */
610
+ Mqtt.prototype.saveOnline = async function (arr, online = 1) {
611
+ if (!arr || !arr.length) {
612
+ return;
613
+ }
614
+ var body = {
615
+ online
616
+ };
617
+ var db = $.admin.sql('sys').db();
618
+ db.table = this.config.table || 'iot_device';
619
+ db.size = 0;
620
+ var query = {};
621
+ query.clientid_has = arr.join(',');
622
+ await db.set(query, body);
623
+ };
624
+
625
+ /**
626
+ * 获取所有设备
627
+ * @returns {Array} 设备列表
628
+ */
629
+ Mqtt.prototype.getClients = async function () {
630
+ var db = $.admin.sql('sys').db();
631
+ db.table = this.config.table || 'iot_device';
632
+ db.size = 0;
633
+ return await db.get({}, '', 'clientid, online');
634
+ };
635
+
636
+ /**
637
+ * 更新在线设备
638
+ */
639
+ Mqtt.prototype.updateOnline = async function () {
640
+ var now = new Date().getTime();
641
+ var online_expires = this.config.online_expires; // 180
642
+
643
+ // 获取所有客户端
644
+ var drives = await this.getClients();
645
+ var { list_online, list_offline, list_has } = this._processDeviceStatus(drives, now, online_expires);
646
+
647
+ // 处理未出现的设备
648
+ // this._processUnseenDevices(drives, list_offline, list_has);
649
+
650
+ // 批量更新设备状态
651
+ await this._updateDeviceStatus(list_offline, list_online);
652
+ };
653
+
654
+ /**
655
+ * 处理设备状态
656
+ * @param {Array} drives 设备列表
657
+ * @param {number} now 当前时间戳
658
+ * @param {number} online_expires 在线过期时间
659
+ * @returns {object} 在线和离线设备列表
660
+ * @private
661
+ */
662
+ Mqtt.prototype._processDeviceStatus = function (drives, now, online_expires) {
663
+ var list_online = [];
664
+ var list_offline = [];
665
+ var list_has = [];
666
+
667
+ var mods = this.getMods();
668
+ for (var k in mods) {
669
+ var o = mods[k];
670
+ var dict = o.drives;
671
+ for (var clientid in dict) {
672
+ list_has.push(clientid);
673
+ var drive = drives.getObj({ clientid });
674
+ var o = dict[clientid];
675
+
676
+ if (o.online === 1) {
677
+ this._onlineDevice(
678
+ o, drive, clientid, now, online_expires,
679
+ list_online, list_offline, dict
680
+ );
681
+ } else {
682
+ this._offlineDevice(drive, clientid, list_offline);
683
+ }
684
+ }
685
+ }
686
+
687
+ return { list_online, list_offline, list_has };
688
+ };
689
+
690
+ /**
691
+ * 处理在线设备
692
+ * @param {object} device 设备对象
693
+ * @param {object} drive 数据库中的设备信息
694
+ * @param {string} clientid 设备ID
695
+ * @param {number} now 当前时间戳
696
+ * @param {number} online_expires 在线过期时间
697
+ * @param {Array} list_online 在线设备列表
698
+ * @param {Array} list_offline 离线设备列表
699
+ * @param {object} dict 设备字典
700
+ * @private
701
+ */
702
+ /* eslint-disable no-param-reassign */
703
+ Mqtt.prototype._onlineDevice = function (
704
+ device, drive, clientid, now, online_expires,
705
+ list_online, list_offline, dict
706
+ ) {
707
+ if (device.time_last) {
708
+ var cha = now - device.time_last;
709
+ if (cha > online_expires) {
710
+ if (drive && drive.online) {
711
+ list_offline.push(clientid);
712
+ }
713
+ // 这里需要修改设备状态,但由于参数修改限制,我们直接修改字典
714
+ // 注意:这里仍然会修改外部对象,但这是业务逻辑需要
715
+ device.online = 0;
716
+ delete dict[clientid];
717
+ } else {
718
+ if (drive && !drive.online) {
719
+ list_online.push(clientid);
720
+ }
721
+ }
722
+ }
723
+ };
724
+ /* eslint-enable no-param-reassign */
725
+
726
+ /**
727
+ * 处理离线设备
728
+ * @param {object} drive 数据库中的设备信息
729
+ * @param {string} clientid 设备ID
730
+ * @param {Array} list_offline 离线设备列表
731
+ * @private
732
+ */
733
+ Mqtt.prototype._offlineDevice = function (drive, clientid, list_offline) {
734
+ if (drive && drive.online) {
735
+ list_offline.push(clientid);
736
+ }
737
+ };
738
+
739
+ /**
740
+ * 处理未出现的设备
741
+ * @param {Array} drives 设备列表
742
+ * @param {Array} list_offline 离线设备列表
743
+ * @param {Array} list_has 已处理的设备ID列表
744
+ * @private
745
+ */
746
+ Mqtt.prototype._processUnseenDevices = function (drives, list_offline, list_has) {
747
+ // 处理未出现的设备
748
+ for (var i = 0; i < drives.length; i++) {
749
+ var { clientid, online } = drives[i];
750
+ if (online && list_has.indexOf(clientid) === -1) {
751
+ list_offline.push(clientid);
752
+ }
753
+ }
754
+ };
755
+
756
+ /**
757
+ * 更新设备状态
758
+ * @param {Array} list_offline 离线设备列表
759
+ * @param {Array} list_online 在线设备列表
760
+ * @private
761
+ */
762
+ Mqtt.prototype._updateDeviceStatus = async function (list_offline, list_online) {
763
+ // 先将设备离线
764
+ var list_off = list_offline.to2D(30);
765
+ for (var i = 0; i < list_off.length; i++) {
766
+ await this.saveOnline(list_off[i], 0);
767
+ }
768
+
769
+ // 再将设备在线
770
+ var list_on = list_online.to2D(30);
771
+ for (var i = 0; i < list_on.length; i++) {
772
+ await this.saveOnline(list_on[i], 1);
773
+ }
774
+ };
775
+
776
+ /**
777
+ * 开始计时器
778
+ */
779
+ Mqtt.prototype.startTimer = function () {
780
+ if (!this.timer) {
781
+ this.timer = setInterval(() => {
782
+ try {
783
+ this.updateOnline();
784
+ } catch (err) {
785
+ console.error('更新在线设备失败', err);
786
+ }
787
+ }, this.config.online_interval);
788
+ }
789
+ };
790
+
791
+ exports.Mqtt = Mqtt;
792
+
793
+
794
+ /**
795
+ * 创建全局管理器
796
+ */
797
+ if (!$.pool.mqtt) {
798
+ $.pool.mqtt = {};
799
+ }
800
+
801
+ function mqttAdmin(scope, title) {
802
+ var sc = scope || $.val.scope + '';
803
+ var obj = $.pool.mqtt[sc];
804
+ if (!obj) {
805
+ $.pool.mqtt[sc] = new Mqtt({
806
+ name: sc,
807
+ title: title
808
+ });
809
+ obj = $.pool.mqtt[sc];
810
+ }
811
+ return obj;
812
+ }
813
+
814
+ /**
815
+ * mqtt管理器, 用于管理插件
816
+ * @param {string} scope 作用域
817
+ * @param {string} title 标题
818
+ * @returns {object} 返回一个缓存类
819
+ */
820
+ if ($.admin) {
821
+ $.admin.mqtt = mqttAdmin;
822
+ }