@things-factory/integration-base 8.0.39 → 8.0.41

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.
@@ -1,18 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const utils_1 = require("@things-factory/utils");
4
- const task_registry_1 = require("../task-registry");
5
- const connection_manager_1 = require("../connection-manager");
4
+ const task_registry_js_1 = require("../task-registry.js");
5
+ const connection_manager_js_1 = require("../connection-manager.js");
6
6
  async function MqttPublish(step, { logger, data, domain }) {
7
- var { connection: connectionName, params: { topic, accessor } } = step;
8
- const { client } = connection_manager_1.ConnectionManager.getConnectionInstanceByName(domain, connectionName);
7
+ var { connection: connectionName, params: { topic, accessor, dataFormat = 'json' } } = step;
8
+ const { client } = connection_manager_js_1.ConnectionManager.getConnectionInstanceByName(domain, connectionName);
9
9
  if (!client) {
10
10
  throw Error(`connection is not found : ${connectionName}`);
11
11
  }
12
12
  if (!topic || !accessor) {
13
13
  throw Error(`topic and accessor should be defined: : topic - '${topic}', accessor - '${accessor}'`);
14
14
  }
15
- var message = JSON.stringify((0, utils_1.access)(accessor, data));
15
+ var message = (0, utils_1.access)(accessor, data);
16
+ if (dataFormat === 'text') {
17
+ message = String(message);
18
+ }
19
+ else {
20
+ message = JSON.stringify(message);
21
+ }
16
22
  await client.publish(topic, message);
17
23
  return {
18
24
  data: message
@@ -28,8 +34,25 @@ MqttPublish.parameterSpec = [
28
34
  type: 'scenario-step-input',
29
35
  name: 'accessor',
30
36
  label: 'accessor'
37
+ },
38
+ {
39
+ type: 'select',
40
+ label: 'data-format',
41
+ name: 'dataFormat',
42
+ property: {
43
+ options: [
44
+ {
45
+ display: 'Plain Text',
46
+ value: 'text'
47
+ },
48
+ {
49
+ display: 'JSON',
50
+ value: 'json'
51
+ }
52
+ ]
53
+ }
31
54
  }
32
55
  ];
33
56
  MqttPublish.help = 'integration/task/mqtt-publish';
34
- task_registry_1.TaskRegistry.registerTaskHandler('mqtt-publish', MqttPublish);
57
+ task_registry_js_1.TaskRegistry.registerTaskHandler('mqtt-publish', MqttPublish);
35
58
  //# sourceMappingURL=mqtt-publish.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mqtt-publish.js","sourceRoot":"","sources":["../../../server/engine/task/mqtt-publish.ts"],"names":[],"mappings":";;AAAA,iDAA8C;AAC9C,oDAA+C;AAC/C,8DAAyD;AAIzD,KAAK,UAAU,WAAW,CAAC,IAAe,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAW;IAC3E,IAAI,EACF,UAAU,EAAE,cAAc,EAC1B,MAAM,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,EAC5B,GAAG,IAAI,CAAA;IAER,MAAM,EAAE,MAAM,EAAE,GAAG,sCAAiB,CAAC,2BAA2B,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IACxF,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,KAAK,CAAC,6BAA6B,cAAc,EAAE,CAAC,CAAA;IAC5D,CAAC;IAED,IAAI,CAAC,KAAK,IAAI,CAAC,QAAQ,EAAE,CAAC;QACxB,MAAM,KAAK,CAAC,oDAAoD,KAAK,kBAAkB,QAAQ,GAAG,CAAC,CAAA;IACrG,CAAC;IAED,IAAI,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,IAAA,cAAM,EAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAA;IACpD,MAAM,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAEpC,OAAO;QACL,IAAI,EAAE,OAAO;KACd,CAAA;AACH,CAAC;AAED,WAAW,CAAC,aAAa,GAAG;IAC1B;QACE,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,OAAO;KACf;IACD;QACE,IAAI,EAAE,qBAAqB;QAC3B,IAAI,EAAE,UAAU;QAChB,KAAK,EAAE,UAAU;KAClB;CACF,CAAA;AAED,WAAW,CAAC,IAAI,GAAG,+BAA+B,CAAA;AAElD,4BAAY,CAAC,mBAAmB,CAAC,cAAc,EAAE,WAAW,CAAC,CAAA","sourcesContent":["import { access } from '@things-factory/utils'\nimport { TaskRegistry } from '../task-registry'\nimport { ConnectionManager } from '../connection-manager'\nimport { InputStep } from '../../service/step/step-type'\nimport { Context } from '../types'\n\nasync function MqttPublish(step: InputStep, { logger, data, domain }: Context) {\n var {\n connection: connectionName,\n params: { topic, accessor }\n } = step\n\n const { client } = ConnectionManager.getConnectionInstanceByName(domain, connectionName)\n if (!client) {\n throw Error(`connection is not found : ${connectionName}`)\n }\n\n if (!topic || !accessor) {\n throw Error(`topic and accessor should be defined: : topic - '${topic}', accessor - '${accessor}'`)\n }\n\n var message = JSON.stringify(access(accessor, data))\n await client.publish(topic, message)\n\n return {\n data: message\n }\n}\n\nMqttPublish.parameterSpec = [\n {\n type: 'string',\n name: 'topic',\n label: 'topic'\n },\n {\n type: 'scenario-step-input',\n name: 'accessor',\n label: 'accessor'\n }\n]\n\nMqttPublish.help = 'integration/task/mqtt-publish'\n\nTaskRegistry.registerTaskHandler('mqtt-publish', MqttPublish)\n"]}
1
+ {"version":3,"file":"mqtt-publish.js","sourceRoot":"","sources":["../../../server/engine/task/mqtt-publish.ts"],"names":[],"mappings":";;AAAA,iDAA8C;AAC9C,0DAAkD;AAClD,oEAA4D;AAI5D,KAAK,UAAU,WAAW,CAAC,IAAe,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAW;IAC3E,IAAI,EACF,UAAU,EAAE,cAAc,EAC1B,MAAM,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,UAAU,GAAG,MAAM,EAAE,EACjD,GAAG,IAAI,CAAA;IAER,MAAM,EAAE,MAAM,EAAE,GAAG,yCAAiB,CAAC,2BAA2B,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IACxF,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,KAAK,CAAC,6BAA6B,cAAc,EAAE,CAAC,CAAA;IAC5D,CAAC;IAED,IAAI,CAAC,KAAK,IAAI,CAAC,QAAQ,EAAE,CAAC;QACxB,MAAM,KAAK,CAAC,oDAAoD,KAAK,kBAAkB,QAAQ,GAAG,CAAC,CAAA;IACrG,CAAC;IAED,IAAI,OAAO,GAAG,IAAA,cAAM,EAAC,QAAQ,EAAE,IAAI,CAAC,CAAA;IAEpC,IAAI,UAAU,KAAK,MAAM,EAAE,CAAC;QAC1B,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,CAAA;IAC3B,CAAC;SAAM,CAAC;QACN,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;IACnC,CAAC;IAED,MAAM,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAEpC,OAAO;QACL,IAAI,EAAE,OAAO;KACd,CAAA;AACH,CAAC;AAED,WAAW,CAAC,aAAa,GAAG;IAC1B;QACE,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,OAAO;KACf;IACD;QACE,IAAI,EAAE,qBAAqB;QAC3B,IAAI,EAAE,UAAU;QAChB,KAAK,EAAE,UAAU;KAClB;IACD;QACE,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,aAAa;QACpB,IAAI,EAAE,YAAY;QAClB,QAAQ,EAAE;YACR,OAAO,EAAE;gBACP;oBACE,OAAO,EAAE,YAAY;oBACrB,KAAK,EAAE,MAAM;iBACd;gBACD;oBACE,OAAO,EAAE,MAAM;oBACf,KAAK,EAAE,MAAM;iBACd;aACF;SACF;KACF;CACF,CAAA;AAED,WAAW,CAAC,IAAI,GAAG,+BAA+B,CAAA;AAElD,+BAAY,CAAC,mBAAmB,CAAC,cAAc,EAAE,WAAW,CAAC,CAAA","sourcesContent":["import { access } from '@things-factory/utils'\nimport { TaskRegistry } from '../task-registry.js'\nimport { ConnectionManager } from '../connection-manager.js'\nimport { InputStep } from '../../service/step/step-type.js'\nimport { Context } from '../types.js'\n\nasync function MqttPublish(step: InputStep, { logger, data, domain }: Context) {\n var {\n connection: connectionName,\n params: { topic, accessor, dataFormat = 'json' }\n } = step\n\n const { client } = ConnectionManager.getConnectionInstanceByName(domain, connectionName)\n if (!client) {\n throw Error(`connection is not found : ${connectionName}`)\n }\n\n if (!topic || !accessor) {\n throw Error(`topic and accessor should be defined: : topic - '${topic}', accessor - '${accessor}'`)\n }\n\n var message = access(accessor, data)\n\n if (dataFormat === 'text') {\n message = String(message)\n } else {\n message = JSON.stringify(message)\n }\n\n await client.publish(topic, message)\n\n return {\n data: message\n }\n}\n\nMqttPublish.parameterSpec = [\n {\n type: 'string',\n name: 'topic',\n label: 'topic'\n },\n {\n type: 'scenario-step-input',\n name: 'accessor',\n label: 'accessor'\n },\n {\n type: 'select',\n label: 'data-format',\n name: 'dataFormat',\n property: {\n options: [\n {\n display: 'Plain Text',\n value: 'text'\n },\n {\n display: 'JSON',\n value: 'json'\n }\n ]\n }\n }\n]\n\nMqttPublish.help = 'integration/task/mqtt-publish'\n\nTaskRegistry.registerTaskHandler('mqtt-publish', MqttPublish)\n"]}
@@ -2,87 +2,201 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  const async_mqtt_1 = tslib_1.__importDefault(require("async-mqtt"));
5
- const task_registry_1 = require("../task-registry");
6
- const connection_manager_1 = require("../connection-manager");
7
- const utils_1 = require("@things-factory/utils");
5
+ const task_registry_js_1 = require("../task-registry.js");
6
+ const connection_manager_js_1 = require("../connection-manager.js");
8
7
  function convertDataFormat(data, format) {
9
8
  if (format == 'json') {
10
- return JSON.parse(data);
9
+ try {
10
+ return JSON.parse(data);
11
+ }
12
+ catch (e) {
13
+ console.error('JSON 파싱 오류:', e.message);
14
+ return data.toString();
15
+ }
11
16
  }
12
17
  else {
13
18
  return data.toString();
14
19
  }
15
20
  }
16
- async function MqttSubscribe(step, context) {
17
- const { connection: connectionName, params: { topic, dataFormat }, name } = step;
18
- const { domain, logger, closures, __mqtt_subscriber } = context;
19
- if (!__mqtt_subscriber) {
20
- context.__mqtt_subscriber = {};
21
+ // MQTT 연결을 위한 브로커 관리 클래스
22
+ class MqttBrokerManager {
23
+ // 브로커 연결 (또는 기존 연결 반환)
24
+ static async getBroker(uri, options) {
25
+ const brokerKey = `${uri}_${JSON.stringify(options || {})}`;
26
+ if (!this.brokers[brokerKey]) {
27
+ const client = await async_mqtt_1.default.connectAsync(uri, options);
28
+ this.brokers[brokerKey] = {
29
+ client,
30
+ topics: new Set(),
31
+ messageHandlers: new Map()
32
+ };
33
+ // 메시지 수신 핸들러
34
+ client.on('message', (topic, message) => {
35
+ // 해당 토픽에 등록된 핸들러가 있으면 호출
36
+ this.brokers[brokerKey].messageHandlers.forEach((handler, handlerId) => {
37
+ if (handlerId.startsWith(`${topic}:`)) {
38
+ handler(topic, message);
39
+ }
40
+ });
41
+ });
42
+ }
43
+ return this.brokers[brokerKey];
44
+ }
45
+ // 토픽 구독 등록
46
+ static async subscribe(brokerKey, topic) {
47
+ const broker = this.brokers[brokerKey];
48
+ if (!broker) {
49
+ throw new Error(`브로커가 연결되지 않음: ${brokerKey}`);
50
+ }
51
+ // 새 토픽인 경우 구독
52
+ if (!broker.topics.has(topic)) {
53
+ await broker.client.subscribe(topic);
54
+ broker.topics.add(topic);
55
+ }
56
+ }
57
+ // 메시지 핸들러 등록
58
+ static registerMessageHandler(brokerKey, topic, handlerId, handler) {
59
+ const broker = this.brokers[brokerKey];
60
+ if (!broker) {
61
+ throw new Error(`브로커가 연결되지 않음: ${brokerKey}`);
62
+ }
63
+ // 핸들러 ID는 topic:handlerId 형식으로 저장
64
+ const fullHandlerId = `${topic}:${handlerId}`;
65
+ broker.messageHandlers.set(fullHandlerId, handler);
66
+ return () => {
67
+ // 핸들러 제거 함수 반환
68
+ broker.messageHandlers.delete(fullHandlerId);
69
+ };
70
+ }
71
+ // 연결 종료
72
+ static async disconnect(brokerKey) {
73
+ const broker = this.brokers[brokerKey];
74
+ if (broker) {
75
+ await broker.client.end();
76
+ delete this.brokers[brokerKey];
77
+ }
21
78
  }
22
- const { connection: { endpoint: uri, params: { user, password } } } = connection_manager_1.ConnectionManager.getConnectionInstanceByName(domain, connectionName);
79
+ // 브로커 생성 유틸리티
80
+ static getBrokerKey(uri, options) {
81
+ return `${uri}_${JSON.stringify(options || {})}`;
82
+ }
83
+ }
84
+ MqttBrokerManager.brokers = {};
85
+ async function MqttSubscribe(step, context) {
86
+ const { connection: connectionName, params: { topic, dataFormat }, name: stepName } = step;
87
+ const { domain, logger, closures } = context;
88
+ // MQTT 브로커 접속 정보 가져오기
89
+ const { connection: { endpoint: uri, params: { user, password } } } = connection_manager_js_1.ConnectionManager.getConnectionInstanceByName(domain, connectionName);
23
90
  if (!topic) {
24
- throw Error(`topic is not found for ${connectionName}`);
91
+ throw Error(`토픽이 지정되지 않음: ${connectionName}`);
25
92
  }
26
- /*
27
- * 1. subscriber list에서 subscriber를 찾는다. 없으면, 생성한다.
28
- * 2. client.once(...)로 메시지를 취한다.
29
- *
30
- * TODO 동일 브로커의 다중 subscribe 태스크에 대해서 완벽한 지원을 해야한다.
31
- * - 현재는 여러 태스크가 동일 topic을 subscribe 하는 경우에 정상동작하지 않을 것이다.
32
- */
33
- if (!context.__mqtt_subscriber[name]) {
34
- try {
35
- var broker = null;
36
- if (user && password) {
37
- broker = await async_mqtt_1.default.connectAsync(uri, { username: user, password: password });
38
- }
39
- else {
40
- broker = await async_mqtt_1.default.connectAsync(uri);
41
- }
42
- logger.info(`mqtt-connector connection(${connectionName}:${uri}) is connected`);
43
- await broker.subscribe(topic);
44
- logger.info(`success subscribing topic '${topic}'`);
45
- var TOPIC;
46
- var MESSAGE;
47
- context.__mqtt_subscriber[name] = async () => {
48
- while (!MESSAGE) {
49
- await (0, utils_1.sleep)(100);
50
- }
51
- var topic = TOPIC;
52
- var message = MESSAGE;
53
- TOPIC = null;
54
- MESSAGE = null;
55
- return {
56
- topic,
57
- message
58
- };
59
- };
60
- broker.on('message', async (messageTopic, message) => {
61
- if (topic !== messageTopic) {
62
- return;
63
- }
64
- TOPIC = topic;
65
- MESSAGE = convertDataFormat(message, dataFormat);
66
- // logger.info(`mqtt-subscribe :\n'${message.toString()}'`)
67
- });
93
+ // 브로커 연결 키 생성
94
+ const connectionOptions = user && password ? { username: user, password } : undefined;
95
+ const brokerKey = MqttBrokerManager.getBrokerKey(uri, connectionOptions);
96
+ // 구독자 ID 생성 (도메인, 연결명, 토픽, 스텝명 조합)
97
+ const subscriberId = `${domain}_${connectionName}_${topic}_${stepName}`;
98
+ try {
99
+ // 브로커 연결 (또는 기존 연결 가져오기)
100
+ await MqttBrokerManager.getBroker(uri, connectionOptions);
101
+ logger.info(`MQTT 연결 완료: ${connectionName}:${uri}`);
102
+ // 토픽 구독 등록
103
+ await MqttBrokerManager.subscribe(brokerKey, topic);
104
+ logger.info(`토픽 구독 완료: ${topic}`);
105
+ // 리졸버 저장소 초기화
106
+ if (!context.__mqtt_resolvers) {
107
+ context.__mqtt_resolvers = new Map();
108
+ }
109
+ // 클로저에 연결 종료 함수 등록
110
+ if (!context.__mqtt_connections) {
111
+ context.__mqtt_connections = new Set();
112
+ }
113
+ if (!context.__mqtt_handlers) {
114
+ context.__mqtt_handlers = new Map();
115
+ }
116
+ // 연결 추적 (중복 종료 방지)
117
+ if (!context.__mqtt_connections.has(brokerKey)) {
118
+ context.__mqtt_connections.add(brokerKey);
119
+ // 연결 종료 함수 등록
68
120
  closures.push(async () => {
69
121
  try {
70
- broker && (await broker.end());
71
- logger.info(`mqtt-connector connection(${connectionName}:${uri}) is disconnected`);
122
+ // 핸들러 모두 제거
123
+ if (context.__mqtt_handlers) {
124
+ context.__mqtt_handlers.forEach(removeHandler => {
125
+ removeHandler();
126
+ });
127
+ context.__mqtt_handlers.clear();
128
+ }
129
+ // 대기 중인 모든 Promise 해결
130
+ if (context.__mqtt_resolvers) {
131
+ context.__mqtt_resolvers.forEach(resolver => {
132
+ resolver({ data: null, terminated: true });
133
+ });
134
+ context.__mqtt_resolvers.clear();
135
+ }
136
+ // 연결 종료
137
+ await MqttBrokerManager.disconnect(brokerKey);
138
+ logger.info(`MQTT 연결 종료: ${connectionName}:${uri}`);
72
139
  }
73
140
  catch (e) {
74
- logger.error(e);
141
+ logger.error(`MQTT 연결 종료 오류: ${e.message}`);
75
142
  }
76
143
  });
77
144
  }
78
- catch (e) {
79
- logger.error(e);
80
- }
145
+ // Promise로 메시지 수신 대기
146
+ return new Promise(resolve => {
147
+ var _a;
148
+ // 이 태스크의 resolver 저장
149
+ if (context.__mqtt_resolvers) {
150
+ context.__mqtt_resolvers.set(subscriberId, resolve);
151
+ }
152
+ // 이미 등록된 핸들러가 있으면 제거
153
+ if ((_a = context.__mqtt_handlers) === null || _a === void 0 ? void 0 : _a.has(subscriberId)) {
154
+ const removeHandler = context.__mqtt_handlers.get(subscriberId);
155
+ if (removeHandler) {
156
+ removeHandler();
157
+ }
158
+ }
159
+ // 새로운 메시지 핸들러 등록
160
+ const removeHandler = MqttBrokerManager.registerMessageHandler(brokerKey, topic, subscriberId, (messageTopic, message) => {
161
+ var _a, _b;
162
+ try {
163
+ // 메시지 변환
164
+ const convertedMessage = convertDataFormat(message, dataFormat);
165
+ // resolver 가져오기 및 삭제
166
+ if ((_a = context.__mqtt_resolvers) === null || _a === void 0 ? void 0 : _a.has(subscriberId)) {
167
+ const resolver = context.__mqtt_resolvers.get(subscriberId);
168
+ context.__mqtt_resolvers.delete(subscriberId);
169
+ // 이 태스크에 대한 핸들러 제거 (한 번만 실행되도록)
170
+ if ((_b = context.__mqtt_handlers) === null || _b === void 0 ? void 0 : _b.has(subscriberId)) {
171
+ const removeHandler = context.__mqtt_handlers.get(subscriberId);
172
+ if (removeHandler) {
173
+ removeHandler();
174
+ }
175
+ context.__mqtt_handlers.delete(subscriberId);
176
+ }
177
+ // Promise 해결
178
+ if (resolver) {
179
+ resolver({
180
+ data: convertedMessage
181
+ });
182
+ }
183
+ }
184
+ }
185
+ catch (error) {
186
+ logger.error(`메시지 처리 오류: ${error.message}`);
187
+ }
188
+ });
189
+ // 핸들러 제거 함수 저장
190
+ if (context.__mqtt_handlers) {
191
+ context.__mqtt_handlers.set(subscriberId, removeHandler);
192
+ }
193
+ logger.info(`MQTT 메시지 대기 중: ${topic}`);
194
+ });
195
+ }
196
+ catch (e) {
197
+ logger.error(`MQTT 구독 오류: ${e.message}`);
198
+ throw e;
81
199
  }
82
- var { message } = await context.__mqtt_subscriber[name]();
83
- return {
84
- data: message
85
- };
86
200
  }
87
201
  MqttSubscribe.parameterSpec = [
88
202
  {
@@ -109,5 +223,5 @@ MqttSubscribe.parameterSpec = [
109
223
  }
110
224
  ];
111
225
  MqttSubscribe.help = 'integration/task/mqtt-subscribe';
112
- task_registry_1.TaskRegistry.registerTaskHandler('mqtt-subscribe', MqttSubscribe);
226
+ task_registry_js_1.TaskRegistry.registerTaskHandler('mqtt-subscribe', MqttSubscribe);
113
227
  //# sourceMappingURL=mqtt-subscribe.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mqtt-subscribe.js","sourceRoot":"","sources":["../../../server/engine/task/mqtt-subscribe.ts"],"names":[],"mappings":";;;AAAA,oEAA6B;AAE7B,oDAA+C;AAC/C,8DAAyD;AACzD,iDAA6C;AAI7C,SAAS,iBAAiB,CAAC,IAAI,EAAE,MAAM;IACrC,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IACzB,CAAC;SAAM,CAAC;QACN,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAA;IACxB,CAAC;AACH,CAAC;AAED,KAAK,UAAU,aAAa,CAAC,IAAe,EAAE,OAAgB;IAC5D,MAAM,EACJ,UAAU,EAAE,cAAc,EAC1B,MAAM,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,EAC7B,IAAI,EACL,GAAG,IAAI,CAAA;IAER,MAAM,EAAE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AAK,EAAE,OAAO;KACf;IACD;QACE,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,aAAa;QACpB,IAAI,EAAE,YAAY;QAClB,QAAQ,EAAE;YACR,OAAO,EAAE;gBACP;oBACE,OAAO,EAAE,YAAY;oBACrB,KAAK,EAAE,MAAM;iBACd;gBACD;oBACE,OAAO,EAAE,MAAM;oBACf,KAAK,EAAE,MAAM;iBACd;aACF;SACF;KACF;CACF,CAAA;AAED,aAAa,CAAC,IAAI,GAAG,iCAAiC,CAAA;AAEtD,4BAAY,CAAC,mBAAmB,CAAC,gBAAgB,EAAE,aAAa,CAAC,CAAA","sourcesContent":["import mqtt from 'async-mqtt'\n\nimport { TaskRegistry } from '../task-registry'\nimport { ConnectionManager } from '../connection-manager'\nimport { sleep } from '@things-factory/utils'\nimport { InputStep } from '../../service/step/step-type'\nimport { Context } from '../types'\n\nfunction convertDataFormat(data, format) {\n if (format == 'json') {\n return JSON.parse(data)\n } else {\n return data.toString()\n }\n}\n\nasync function MqttSubscribe(step: InputStep, context: Context) {\n const {\n connection: connectionName,\n params: { topic, dataFormat },\n name\n } = step\n\n const { domain, logger, closures, __mqtt_subscriber } = context\n if (!__mqtt_subscriber) {\n context.__mqtt_subscriber = {}\n }\n\n const {\n connection: {\n endpoint: uri,\n params: { user, password }\n }\n } = ConnectionManager.getConnectionInstanceByName(domain, connectionName)\n\n if (!topic) {\n throw Error(`topic is not found for ${connectionName}`)\n }\n\n /*\n * 1. subscriber list에서 subscriber를 찾는다. 없으면, 생성한다.\n * 2. client.once(...)로 메시지를 취한다.\n *\n * TODO 동일 브로커의 다중 subscribe 태스크에 대해서 완벽한 지원을 해야한다.\n * - 현재는 여러 태스크가 동일 topic을 subscribe 하는 경우에 정상동작하지 않을 것이다.\n */\n if (!context.__mqtt_subscriber[name]) {\n try {\n var broker = null\n if (user && password) {\n broker = await mqtt.connectAsync(uri, { username: user, password: password })\n } else {\n broker = await mqtt.connectAsync(uri)\n }\n\n logger.info(`mqtt-connector connection(${connectionName}:${uri}) is connected`)\n\n await broker.subscribe(topic)\n logger.info(`success subscribing topic '${topic}'`)\n\n var TOPIC\n var MESSAGE\n\n context.__mqtt_subscriber[name] = async () => {\n while (!MESSAGE) {\n await sleep(100)\n }\n\n var topic = TOPIC\n var message = MESSAGE\n\n TOPIC = null\n MESSAGE = null\n\n return {\n topic,\n message\n }\n }\n\n broker.on('message', async (messageTopic, message) => {\n if (topic !== messageTopic) {\n return\n }\n\n TOPIC = topic\n MESSAGE = convertDataFormat(message, dataFormat)\n\n // logger.info(`mqtt-subscribe :\\n'${message.toString()}'`)\n })\n\n closures.push(async () => {\n try {\n broker && (await broker.end())\n logger.info(`mqtt-connector connection(${connectionName}:${uri}) is disconnected`)\n } catch (e) {\n logger.error(e)\n }\n })\n } catch (e) {\n logger.error(e)\n }\n }\n\n var { message } = await context.__mqtt_subscriber[name]()\n\n return {\n data: message\n }\n}\n\nMqttSubscribe.parameterSpec = [\n {\n type: 'string',\n name: 'topic',\n label: 'topic'\n },\n {\n type: 'select',\n label: 'data-format',\n name: 'dataFormat',\n property: {\n options: [\n {\n display: 'Plain Text',\n value: 'text'\n },\n {\n display: 'JSON',\n value: 'json'\n }\n ]\n }\n }\n]\n\nMqttSubscribe.help = 'integration/task/mqtt-subscribe'\n\nTaskRegistry.registerTaskHandler('mqtt-subscribe', MqttSubscribe)\n"]}
1
+ {"version":3,"file":"mqtt-subscribe.js","sourceRoot":"","sources":["../../../server/engine/task/mqtt-subscribe.ts"],"names":[],"mappings":";;;AAAA,oEAA6B;AAE7B,0DAAkD;AAClD,oEAA4D;AAI5D,SAAS,iBAAiB,CAAC,IAAI,EAAE,MAAM;IACrC,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;QACrB,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACzB,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,CAAC,KAAK,CAAC,aAAa,EAAE,CAA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AAO;KACf;IACD;QACE,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,aAAa;QACpB,IAAI,EAAE,YAAY;QAClB,QAAQ,EAAE;YACR,OAAO,EAAE;gBACP;oBACE,OAAO,EAAE,YAAY;oBACrB,KAAK,EAAE,MAAM;iBACd;gBACD;oBACE,OAAO,EAAE,MAAM;oBACf,KAAK,EAAE,MAAM;iBACd;aACF;SACF;KACF;CACF,CAAA;AAED,aAAa,CAAC,IAAI,GAAG,iCAAiC,CAAA;AAEtD,+BAAY,CAAC,mBAAmB,CAAC,gBAAgB,EAAE,aAAa,CAAC,CAAA","sourcesContent":["import mqtt from 'async-mqtt'\n\nimport { TaskRegistry } from '../task-registry.js'\nimport { ConnectionManager } from '../connection-manager.js'\nimport { InputStep } from '../../service/step/step-type.js'\nimport { Context } from '../types.js'\n\nfunction convertDataFormat(data, format) {\n if (format == 'json') {\n try {\n return JSON.parse(data)\n } catch (e) {\n console.error('JSON 파싱 오류:', e.message)\n return data.toString()\n }\n } else {\n return data.toString()\n }\n}\n\n// MQTT 연결을 위한 브로커 관리 클래스\nclass MqttBrokerManager {\n private static brokers: Record<\n string,\n {\n client: mqtt.AsyncMqttClient\n topics: Set<string>\n messageHandlers: Map<string, (topic: string, message: Buffer) => void>\n }\n > = {}\n\n // 브로커 연결 (또는 기존 연결 반환)\n static async getBroker(uri: string, options?: mqtt.IClientOptions) {\n const brokerKey = `${uri}_${JSON.stringify(options || {})}`\n\n if (!this.brokers[brokerKey]) {\n const client = await mqtt.connectAsync(uri, options)\n\n this.brokers[brokerKey] = {\n client,\n topics: new Set<string>(),\n messageHandlers: new Map()\n }\n\n // 메시지 수신 핸들러\n client.on('message', (topic, message) => {\n // 해당 토픽에 등록된 핸들러가 있으면 호출\n this.brokers[brokerKey].messageHandlers.forEach((handler, handlerId) => {\n if (handlerId.startsWith(`${topic}:`)) {\n handler(topic, message)\n }\n })\n })\n }\n\n return this.brokers[brokerKey]\n }\n\n // 토픽 구독 등록\n static async subscribe(brokerKey: string, topic: string) {\n const broker = this.brokers[brokerKey]\n if (!broker) {\n throw new Error(`브로커가 연결되지 않음: ${brokerKey}`)\n }\n\n // 새 토픽인 경우 구독\n if (!broker.topics.has(topic)) {\n await broker.client.subscribe(topic)\n broker.topics.add(topic)\n }\n }\n\n // 메시지 핸들러 등록\n static registerMessageHandler(\n brokerKey: string,\n topic: string,\n handlerId: string,\n handler: (topic: string, message: Buffer) => void\n ) {\n const broker = this.brokers[brokerKey]\n if (!broker) {\n throw new Error(`브로커가 연결되지 않음: ${brokerKey}`)\n }\n\n // 핸들러 ID는 topic:handlerId 형식으로 저장\n const fullHandlerId = `${topic}:${handlerId}`\n broker.messageHandlers.set(fullHandlerId, handler)\n\n return () => {\n // 핸들러 제거 함수 반환\n broker.messageHandlers.delete(fullHandlerId)\n }\n }\n\n // 연결 종료\n static async disconnect(brokerKey: string) {\n const broker = this.brokers[brokerKey]\n if (broker) {\n await broker.client.end()\n delete this.brokers[brokerKey]\n }\n }\n\n // 브로커 키 생성 유틸리티\n static getBrokerKey(uri: string, options?: any) {\n return `${uri}_${JSON.stringify(options || {})}`\n }\n}\n\ninterface MqttContext extends Context {\n __mqtt_connections?: Set<string>\n __mqtt_handlers?: Map<string, () => void>\n __mqtt_resolvers?: Map<string, (result: any) => void>\n}\n\nasync function MqttSubscribe(step: InputStep, context: MqttContext) {\n const {\n connection: connectionName,\n params: { topic, dataFormat },\n name: stepName\n } = step\n\n const { domain, logger, closures } = context\n\n // MQTT 브로커 접속 정보 가져오기\n const {\n connection: {\n endpoint: uri,\n params: { user, password }\n }\n } = ConnectionManager.getConnectionInstanceByName(domain, connectionName)\n\n if (!topic) {\n throw Error(`토픽이 지정되지 않음: ${connectionName}`)\n }\n\n // 브로커 연결 키 생성\n const connectionOptions = user && password ? { username: user, password } : undefined\n const brokerKey = MqttBrokerManager.getBrokerKey(uri, connectionOptions)\n\n // 구독자 ID 생성 (도메인, 연결명, 토픽, 스텝명 조합)\n const subscriberId = `${domain}_${connectionName}_${topic}_${stepName}`\n\n try {\n // 브로커 연결 (또는 기존 연결 가져오기)\n await MqttBrokerManager.getBroker(uri, connectionOptions)\n logger.info(`MQTT 연결 완료: ${connectionName}:${uri}`)\n\n // 토픽 구독 등록\n await MqttBrokerManager.subscribe(brokerKey, topic)\n logger.info(`토픽 구독 완료: ${topic}`)\n\n // 리졸버 저장소 초기화\n if (!context.__mqtt_resolvers) {\n context.__mqtt_resolvers = new Map()\n }\n\n // 클로저에 연결 종료 함수 등록\n if (!context.__mqtt_connections) {\n context.__mqtt_connections = new Set()\n }\n\n if (!context.__mqtt_handlers) {\n context.__mqtt_handlers = new Map()\n }\n\n // 연결 추적 (중복 종료 방지)\n if (!context.__mqtt_connections.has(brokerKey)) {\n context.__mqtt_connections.add(brokerKey)\n\n // 연결 종료 함수 등록\n closures.push(async () => {\n try {\n // 핸들러 모두 제거\n if (context.__mqtt_handlers) {\n context.__mqtt_handlers.forEach(removeHandler => {\n removeHandler()\n })\n context.__mqtt_handlers.clear()\n }\n\n // 대기 중인 모든 Promise 해결\n if (context.__mqtt_resolvers) {\n context.__mqtt_resolvers.forEach(resolver => {\n resolver({ data: null, terminated: true })\n })\n context.__mqtt_resolvers.clear()\n }\n\n // 연결 종료\n await MqttBrokerManager.disconnect(brokerKey)\n logger.info(`MQTT 연결 종료: ${connectionName}:${uri}`)\n } catch (e) {\n logger.error(`MQTT 연결 종료 오류: ${e.message}`)\n }\n })\n }\n\n // Promise로 메시지 수신 대기\n return new Promise(resolve => {\n // 이 태스크의 resolver 저장\n if (context.__mqtt_resolvers) {\n context.__mqtt_resolvers.set(subscriberId, resolve)\n }\n\n // 이미 등록된 핸들러가 있으면 제거\n if (context.__mqtt_handlers?.has(subscriberId)) {\n const removeHandler = context.__mqtt_handlers.get(subscriberId)\n if (removeHandler) {\n removeHandler()\n }\n }\n\n // 새로운 메시지 핸들러 등록\n const removeHandler = MqttBrokerManager.registerMessageHandler(\n brokerKey,\n topic,\n subscriberId,\n (messageTopic, message) => {\n try {\n // 메시지 변환\n const convertedMessage = convertDataFormat(message, dataFormat)\n\n // resolver 가져오기 및 삭제\n if (context.__mqtt_resolvers?.has(subscriberId)) {\n const resolver = context.__mqtt_resolvers.get(subscriberId)\n context.__mqtt_resolvers.delete(subscriberId)\n\n // 이 태스크에 대한 핸들러 제거 (한 번만 실행되도록)\n if (context.__mqtt_handlers?.has(subscriberId)) {\n const removeHandler = context.__mqtt_handlers.get(subscriberId)\n if (removeHandler) {\n removeHandler()\n }\n context.__mqtt_handlers.delete(subscriberId)\n }\n\n // Promise 해결\n if (resolver) {\n resolver({\n data: convertedMessage\n })\n }\n }\n } catch (error) {\n logger.error(`메시지 처리 오류: ${error.message}`)\n }\n }\n )\n\n // 핸들러 제거 함수 저장\n if (context.__mqtt_handlers) {\n context.__mqtt_handlers.set(subscriberId, removeHandler)\n }\n\n logger.info(`MQTT 메시지 대기 중: ${topic}`)\n })\n } catch (e) {\n logger.error(`MQTT 구독 오류: ${e.message}`)\n throw e\n }\n}\n\nMqttSubscribe.parameterSpec = [\n {\n type: 'string',\n name: 'topic',\n label: 'topic'\n },\n {\n type: 'select',\n label: 'data-format',\n name: 'dataFormat',\n property: {\n options: [\n {\n display: 'Plain Text',\n value: 'text'\n },\n {\n display: 'JSON',\n value: 'json'\n }\n ]\n }\n }\n]\n\nMqttSubscribe.help = 'integration/task/mqtt-subscribe'\n\nTaskRegistry.registerTaskHandler('mqtt-subscribe', MqttSubscribe)\n"]}
@@ -11,7 +11,7 @@ export * from './payload-log/payload-log';
11
11
  export * from './state-register/state-register';
12
12
  export declare const entities: any[];
13
13
  export declare const schema: {
14
- resolverClasses: (typeof import("./task-type/task-type-query").TaskTypeQuery | typeof import("./connector/connector-query").ConnectorQuery | typeof import("./connection/connection-query").ConnectionQuery | typeof import("./connection/connection-mutation").ConnectionMutation | typeof import("./connection/connection-subscription").ConnectionSubscription | typeof import("./scenario/scenario-query").ScenarioQuery | typeof import("./scenario/scenario-mutation").ScenarioMutation | typeof import("./scenario-instance/scenario-instance-query").ScenarioInstanceQuery | typeof import("./scenario-instance/scenario-instance-mutation").ScenarioInstanceMutation | typeof import("./scenario-instance/scenario-instance-subscription").ScenarioInstanceSubscription | typeof import("./scenario-queue/scenario-queue-subscription").ScenarioQueueSubscription | typeof import("./step/step-query").StepQuery | typeof import("./step/step-mutation").StepMutation | typeof import("./payload-log/payload-log-query").PayloadLogQuery | typeof import("./payload-log/payload-log-mutation").PayloadLogMutation | typeof import("./analysis/analysis-query").IntegrationAnalysisQuery | typeof import("./state-register/state-register-query").StateRegisterQuery | typeof import("./state-register/state-register-mutation").StateRegisterMutation | typeof import("./state-register/data-resolver").DataResolver)[];
14
+ resolverClasses: (typeof import("./connection/connection-query").ConnectionQuery | typeof import("./connection/connection-mutation").ConnectionMutation | typeof import("./connection/connection-subscription").ConnectionSubscription | typeof import("./connector/connector-query").ConnectorQuery | typeof import("./scenario/scenario-query").ScenarioQuery | typeof import("./scenario/scenario-mutation").ScenarioMutation | typeof import("./scenario-instance/scenario-instance-query").ScenarioInstanceQuery | typeof import("./scenario-instance/scenario-instance-mutation").ScenarioInstanceMutation | typeof import("./scenario-instance/scenario-instance-subscription").ScenarioInstanceSubscription | typeof import("./scenario-queue/scenario-queue-subscription").ScenarioQueueSubscription | typeof import("./step/step-query").StepQuery | typeof import("./step/step-mutation").StepMutation | typeof import("./task-type/task-type-query").TaskTypeQuery | typeof import("./payload-log/payload-log-query").PayloadLogQuery | typeof import("./payload-log/payload-log-mutation").PayloadLogMutation | typeof import("./state-register/state-register-query").StateRegisterQuery | typeof import("./state-register/state-register-mutation").StateRegisterMutation | typeof import("./state-register/data-resolver").DataResolver | typeof import("./analysis/analysis-query").IntegrationAnalysisQuery)[];
15
15
  };
16
16
  export { PayloadType } from './payload-log/payload-log';
17
17
  export { createPayloadLog } from './payload-log/payload-log-mutation';