|
@@ -40,6 +40,22 @@ import { DeviceView } from '../view/DeviceView';
|
|
|
|
|
|
import common from '@ohos.app.ability.common';
|
|
|
import { WorkshopView } from '../view/WorkshopView';
|
|
|
+import CommonConstants2 from '../common/constants/CommonConstants'
|
|
|
+import {
|
|
|
+ MqttAsync,
|
|
|
+ MqttConnectOptions,
|
|
|
+ MqttSubscribeOptions,
|
|
|
+ MqttPublishOptions,
|
|
|
+ MqttResponse,
|
|
|
+ MqttMessage,
|
|
|
+ MqttClient,
|
|
|
+ MqttClientOptions,
|
|
|
+} from '@ohos/mqtt';
|
|
|
+import MqttManager from '../common/util/mqtt';
|
|
|
+import MqttUploadData from '../viewmodel/mqtt/MqttUploadData'
|
|
|
+import ElectricScrewdriverPresetModel from '../model/ElectricScrewdriverPresetModel';
|
|
|
+import ElectricSolderingIronPresetModel from '../model/ElectricSolderingIronPresetModel';
|
|
|
+import connection from '@ohos.net.connection';
|
|
|
|
|
|
const TAG = "EntryView";
|
|
|
|
|
@@ -69,6 +85,10 @@ struct EntryView {
|
|
|
|
|
|
@State selectNavbar: number = 1
|
|
|
|
|
|
+ // mqtt是否连接
|
|
|
+ @State isConnected: boolean = false
|
|
|
+ @Provide('stationIp') stationIp: string = ''
|
|
|
+
|
|
|
onPageShow(): void {
|
|
|
Log.showInfo(TAG, 'onPageShow');
|
|
|
}
|
|
@@ -79,7 +99,7 @@ struct EntryView {
|
|
|
Log.showInfo(TAG, 'onPageHide');
|
|
|
}
|
|
|
|
|
|
- aboutToAppear(): void {
|
|
|
+ async aboutToAppear() {
|
|
|
Log.showInfo(TAG, 'aboutToAppear');
|
|
|
this.mStage.onCreate();
|
|
|
this.navigationBarStatus = SettingsModel.getInstance().getValue();
|
|
@@ -89,6 +109,38 @@ struct EntryView {
|
|
|
this.registerPageDesktopNavigatorStatusChangeEvent(this.mLocalEventListener);
|
|
|
//register orientation listener
|
|
|
this.mOrientationListener.on('change', this.onPortrait);
|
|
|
+
|
|
|
+ ElectricScrewdriverPresetModel.initTaskDB(getContext())
|
|
|
+ ElectricSolderingIronPresetModel.initTaskDB(getContext())
|
|
|
+ // 获取工位ip
|
|
|
+ this.stationIp = CommonConstants2.STATION_DEFAULT_IP
|
|
|
+ try {
|
|
|
+ const netHandle = connection.getDefaultNetSync();
|
|
|
+ if (netHandle) {
|
|
|
+ let connectionProperties = await connection.getConnectionProperties(netHandle)
|
|
|
+ if (connectionProperties && connectionProperties.linkAddresses) {
|
|
|
+ connectionProperties.linkAddresses.forEach((address: connection.LinkAddress, index: number) => {
|
|
|
+ this.stationIp = address.address.address
|
|
|
+ console.log('hhtest', 'stationIp---------------' + this.stationIp );
|
|
|
+ if (address.address.address.startsWith(CommonConstants2.ipPrefix)) {
|
|
|
+ CommonConstants2.mqttSubscribeTopic1 = 'station' + address.address.address.replace(CommonConstants2.ipPrefix, '') + '/data/devices'
|
|
|
+ CommonConstants2.mqttSubscribePublish = 'station' + address.address.address.replace(CommonConstants2.ipPrefix, '') + '/cmd/devices'
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ console.log('hhtest', err);
|
|
|
+ }
|
|
|
+ // 创建mqtt连接和usb设备连接(每两秒检测是否连接成功)
|
|
|
+ const checkInterval = setInterval(() => {
|
|
|
+ console.log('hhtest', 'isConnected:------' + this.isConnected)
|
|
|
+ if (this.isConnected) {
|
|
|
+ clearInterval(checkInterval); // 停止轮询
|
|
|
+ return
|
|
|
+ }
|
|
|
+ this.connectMQTT()
|
|
|
+ }, 1000);
|
|
|
}
|
|
|
|
|
|
registerPageDesktopNavigatorStatusChangeEvent(listener: LocalEventListener): void {
|
|
@@ -119,6 +171,40 @@ struct EntryView {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+
|
|
|
+ connectMQTT = async ()=> {
|
|
|
+ const clientOptions: MqttClientOptions = {
|
|
|
+ url: CommonConstants2.mqttUrl, // 替换实际IP
|
|
|
+ clientId: `station_${Date.now()}`,
|
|
|
+ persistenceType: 1, // 使用英文逗号 // 建议开启自动重连
|
|
|
+ };
|
|
|
+ // MQTT连接配置
|
|
|
+ const connectOptions: MqttConnectOptions = {
|
|
|
+ cleanSession: true,
|
|
|
+ connectTimeout: 30,
|
|
|
+ keepAliveInterval: 60,
|
|
|
+ userName: CommonConstants2.mqttUserName,
|
|
|
+ password: CommonConstants2.mqttPassword
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ MqttManager.init(clientOptions);
|
|
|
+ this.isConnected = await MqttManager.connect(connectOptions);
|
|
|
+ console.log('hhtest', 'isConnected:------' + this.isConnected)
|
|
|
+ if (this.isConnected) {
|
|
|
+ console.info('hhtest', 'MQTT connected successfully');
|
|
|
+ await MqttManager.subscribe(CommonConstants2.mqttSubscribeTopic1);
|
|
|
+ if (CommonConstants2.mqttSubscribeTopic1 != CommonConstants2.mqttSubscribeTopic2) {
|
|
|
+ await MqttManager.subscribe(CommonConstants2.mqttSubscribeTopic2);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ console.error('hhtest', 'MQTT connection failed');
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ console.error('hhtest', `MQTT error: ${JSON.stringify(err)}`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
aboutToDisappear(): void {
|
|
|
this.mOrientationListener.off('change', this.onPortrait);
|
|
|
this.mStage.onDestroy();
|