11import 'dart:async' ;
22import 'dart:convert' ;
33
4+ import 'package:bonsoir/bonsoir.dart' ;
45import 'package:flutter/material.dart' ;
56import 'package:flutter/services.dart' ;
6- import 'package:oktoast/oktoast.dart' ;
77import 'package:openiothub_api/openiothub_api.dart' ;
88import 'package:openiothub_common_pages/openiothub_common_pages.dart' ;
99import 'package:openiothub_constants/openiothub_constants.dart' ;
@@ -14,8 +14,6 @@ import 'package:openiothub_grpc_api/proto/manager/serverManager.pb.dart';
1414import 'package:openiothub_grpc_api/proto/mobile/mobile.pb.dart' ;
1515import 'package:openiothub_grpc_api/proto/mobile/mobile.pbgrpc.dart' ;
1616import 'package:openiothub_plugin/plugins/mdnsService/components.dart' ;
17-
18- import 'package:bonsoir/bonsoir.dart' ;
1917import 'package:tdesign_flutter/tdesign_flutter.dart' ;
2018
2119const utf8encoder = Utf8Encoder ();
@@ -30,6 +28,7 @@ class FindGatewayGoListPage extends StatefulWidget {
3028class _FindGatewayGoListPageState extends State <FindGatewayGoListPage > {
3129 BonsoirDiscovery ? action;
3230 final Map <String , PortService > _ServiceMap = {};
31+
3332 // final flutterNsd = FlutterNsd();
3433 bool initialStart = true ;
3534 bool _scanning = false ;
@@ -42,6 +41,12 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
4241 startDiscovery ();
4342 }
4443
44+ @override
45+ void dispose () {
46+ action! .stop ();
47+ super .dispose ();
48+ }
49+
4550 Future <void > startDiscovery () async {
4651 if (_scanning) return ;
4752
@@ -68,23 +73,26 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
6873 // services.add(service);
6974 // print(oneMdnsService);
7075 oneMdnsService.resolve (action! .serviceResolver);
71- } else if (event.type == BonsoirDiscoveryEventType .discoveryServiceResolved) {
76+ } else if (event.type ==
77+ BonsoirDiscoveryEventType .discoveryServiceResolved) {
7278 // print(oneMdnsService);
7379 // services.removeWhere((foundService) => foundService.name == service.name);
7480 // services.add(service);
7581 setState (() {
7682 PortService _portService = PortService .create ();
77- _portService.ip = (oneMdnsService as ResolvedBonsoirService ).host! .replaceAll (RegExp (r'.local.' ), ".local" );
83+ _portService.ip = (oneMdnsService as ResolvedBonsoirService )
84+ .host!
85+ .replaceAll (RegExp (r'.local.local.' ), ".local" )
86+ .replaceAll (RegExp (r'.local.' ), ".local" );
7887 print (_portService.ip);
7988 _portService.port = oneMdnsService.port;
8089 _portService.isLocal = true ;
8190 _portService.info.addAll ({
82- "name" : "${oneMdnsService .name }(${_portService .ip }:${oneMdnsService .port })" ,
91+ "name" :
92+ "${oneMdnsService .name }(${_portService .ip }:${oneMdnsService .port })" ,
8393 "model" : Gateway .modelName,
8494 "mac" : "mac" ,
85- "id" : _portService.ip +
86- ":" +
87- _portService.port.toString (),
95+ "id" : _portService.ip + ":" + _portService.port.toString (),
8896 "author" : "Farry" ,
8997 "email" : "newfarry@126.com" ,
9098 "home-page" : "https://github.com/OpenIoTHub" ,
@@ -122,37 +130,69 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
122130 final tiles = _ServiceMap .values.map (
123131 (pair) {
124132 var listItemContent = ListTile (
125- leading: Icon (Icons .devices,
126- color: Colors .green),
133+ leading: Icon (TDIcons .earth, color: Colors .green),
127134 title: Row (
128135 mainAxisAlignment: MainAxisAlignment .start,
129136 children: < Widget > [
130- Text (
131- '${pair .ip }:${pair .port }' ,
132- style: Constants .titleTextStyle),
137+ Text ('${pair .ip }:${pair .port }' , style: Constants .titleTextStyle),
133138 ],
134139 ),
135140 subtitle: TDTag (
136- "${pair .info ["version" ]!= null ? pair .info ["version" ]: pair .info ["firmware-version" ]}" ,
141+ "version: ${pair .info ["version" ] != null ? pair .info ["version" ] : pair .info ["firmware-version" ]}" ,
137142 theme: TDTagTheme .success,
138143 // isOutline: true,
139144 isLight: true ,
140145 fixedWidth: 100 ,
141146 ),
142147 trailing: Constants .rightArrowIcon,
143- );
144- return InkWell (
145148 onTap: () {
149+ TextEditingController nameController =
150+ TextEditingController .fromValue (
151+ TextEditingValue (text: "Gateway-${DateTime .now ().minute }" ));
152+ TextEditingController descriptionController =
153+ TextEditingController .fromValue (
154+ TextEditingValue (text: "Gateway-${DateTime .now ()}" ));
146155 // 对于mdns含有添加信息的,直接在本页面使用api添加
147- if (pair.info.containsKey ("run_id" ) && ! pair.info["run_id" ]! .isEmpty) {
148- // TODO 确认添加
156+ if (pair.info.containsKey ("run_id" ) &&
157+ ! pair.info["run_id" ]! .isEmpty) {
158+ // 确认添加
149159 showGeneralDialog (
150160 context: context,
151- pageBuilder: (BuildContext buildContext, Animation <double > animation,
161+ pageBuilder: (BuildContext buildContext,
162+ Animation <double > animation,
152163 Animation <double > secondaryAnimation) {
153164 return TDAlertDialog (
154- title: OpenIoTHubCommonLocalizations .of (context).confirm_add_gateway,
155- content: "" ,
165+ title: OpenIoTHubCommonLocalizations .of (context)
166+ .confirm_add_gateway,
167+ contentWidget: Column (children: < Widget > [
168+ TDInput (
169+ leftLabel:
170+ OpenIoTHubCommonLocalizations .of (context).name,
171+ leftLabelSpace: 0 ,
172+ hintText: "" ,
173+ backgroundColor: Colors .white,
174+ textAlign: TextAlign .left,
175+ showBottomDivider: true ,
176+ controller: nameController,
177+ inputType: TextInputType .text,
178+ maxLines: 1 ,
179+ needClear: true ,
180+ ),
181+ TDInput (
182+ leftLabel: OpenIoTHubCommonLocalizations .of (context)
183+ .description,
184+ leftLabelSpace: 0 ,
185+ hintText: "" ,
186+ backgroundColor: Colors .white,
187+ textAlign: TextAlign .left,
188+ showBottomDivider: true ,
189+ controller: descriptionController,
190+ inputType: TextInputType .text,
191+ maxLines: 1 ,
192+ needClear: true ,
193+ )
194+ // 是否自动添加网关主机
195+ ]),
156196 titleColor: Colors .black,
157197 contentColor: Colors .redAccent,
158198 // backgroundColor: AppTheme.blockBgColor,
@@ -162,7 +202,7 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
162202 style: TDButtonStyle (
163203 backgroundColor: Colors .grey,
164204 ),
165- action: (){
205+ action: () {
166206 Navigator .of (context).pop ();
167207 },
168208 ),
@@ -171,9 +211,13 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
171211 style: TDButtonStyle (
172212 backgroundColor: Colors .blue,
173213 ),
174- action: (){
214+ action: () {
175215 Navigator .of (context).pop ();
176- _addToMyAccount (pair.info["run_id" ]! , pair.info["server_host" ]);
216+ _addToMyAccount (
217+ pair.info["run_id" ]! ,
218+ pair.info["server_host" ],
219+ nameController.text,
220+ descriptionController.text);
177221 },
178222 ),
179223 );
@@ -190,6 +234,8 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
190234 );
191235 }));
192236 },
237+ );
238+ return InkWell (
193239 child: listItemContent,
194240 );
195241 },
@@ -200,7 +246,8 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
200246 ).toList ();
201247 return Scaffold (
202248 appBar: AppBar (
203- title: Text (OpenIoTHubCommonLocalizations .of (context).find_local_gateway_list),
249+ title: Text (
250+ OpenIoTHubCommonLocalizations .of (context).find_local_gateway_list),
204251 actions: < Widget > [
205252 // IconButton(
206253 // icon: Icon(
@@ -241,34 +288,50 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
241288 }
242289
243290 //已经确认过可以添加,添加到我的账号
244- void _addToMyAccount (String gatewayId, String ? host) async {
291+ void _addToMyAccount (
292+ String gatewayId, String ? host, name, description) async {
245293 try {
246294 // TODO 可以搞一个确认步骤,确认后添加
247295 // 使用扫描的Gateway ID构建一个GatewayInfo用于服务器添加
248296 GatewayInfo gatewayInfo = GatewayInfo (
249297 gatewayUuid: gatewayId,
250298 // 服务器的UUID变主机地址,或者都可以
251299 serverUuid: host,
252- name: "Gateway-${ DateTime . now (). minute }" ,
253- description: "Gateway-${ DateTime . now ()}" );
300+ name: name ,
301+ description: description );
254302 OperationResponse operationResponse =
255- await GatewayManager .AddGateway (gatewayInfo);
303+ await GatewayManager .AddGateway (gatewayInfo);
256304 //将网关映射到本机
257305 if (operationResponse.code == 0 ) {
258306 // TODO 从服务器获取连接JWT
259307 StringValue openIoTHubJwt =
260- await GatewayManager .GetOpenIoTHubJwtByGatewayUuid (gatewayId);
308+ await GatewayManager .GetOpenIoTHubJwtByGatewayUuid (gatewayId);
261309 await _addToMySessionList (
262- openIoTHubJwt.value,
263- "Gateway-${DateTime .now ()}" ,
264- "Gateway-${DateTime .now ()} form scan QR code" );
310+ openIoTHubJwt.value, gatewayInfo.name, gatewayInfo.description);
265311 } else {
266- showToast (
267- "Response: ${ operationResponse . msg }" );
312+ TDToast . showText ( "Response: ${ operationResponse . msg }" ,
313+ context : context );
268314 }
315+ //自动 添加网关主机
316+ var device = Device ();
317+ device.runId = getOneUUID ();
318+ device.uuid = getOneUUID ();
319+ device.name = name;
320+ device.description = description;
321+ device.addr = "127.0.0.1" ;
322+ await CommonDeviceApi .createOneDevice (device);
323+ //自动 添加网关界面端口
324+ var tcpConfig = PortConfig ();
325+ tcpConfig.device = device;
326+ tcpConfig.name = "$name Gateway" ;
327+ tcpConfig.description = "$name Gateway" ;
328+ tcpConfig.remotePort = 34323 ;
329+ tcpConfig.localProt = 0 ;
330+ tcpConfig.networkProtocol = "tcp" ;
331+ tcpConfig.applicationProtocol = "http" ;
332+ CommonDeviceApi .createOneUDP (tcpConfig);
269333 } catch (exception) {
270- showToast (
271- "Failed: ${exception }" );
334+ TDToast .showText ("Failed: ${exception }" , context: context);
272335 }
273336 }
274337
@@ -280,17 +343,20 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
280343 builder: (context) {
281344 return StatefulBuilder (builder: (context, state) {
282345 return AlertDialog (
283- title: Text (OpenIoTHubCommonLocalizations .of (context).manually_create_a_gateway),
346+ title: Text (OpenIoTHubCommonLocalizations .of (context)
347+ .manually_create_a_gateway),
284348 content: SizedBox .expand (
285349 child: ListView (
286350 children: ListTile .divideTiles (
287351 context: context,
288352 tiles: [
289- Text (OpenIoTHubCommonLocalizations .of (context).manually_create_a_gateway_description1),
290- Text (
291- OpenIoTHubCommonLocalizations .of (context).manually_create_a_gateway_description2),
353+ Text (OpenIoTHubCommonLocalizations .of (context)
354+ .manually_create_a_gateway_description1),
355+ Text (OpenIoTHubCommonLocalizations .of (context)
356+ .manually_create_a_gateway_description2),
292357 Text (
293- OpenIoTHubCommonLocalizations .of (context).manually_create_a_gateway_description3,
358+ OpenIoTHubCommonLocalizations .of (context)
359+ .manually_create_a_gateway_description3,
294360 style: TextStyle (
295361 color: Colors .amber,
296362 ),
@@ -309,7 +375,8 @@ class _FindGatewayGoListPageState extends State<FindGatewayGoListPage> {
309375 )),
310376 actions: < Widget > [
311377 TextButton (
312- child: Text (OpenIoTHubCommonLocalizations .of (context).cancel),
378+ child:
379+ Text (OpenIoTHubCommonLocalizations .of (context).cancel),
313380 onPressed: () {
314381 Navigator .of (context).pop ();
315382 },
@@ -335,7 +402,9 @@ loginwithtokenmap:
335402 $uuid : $gatewayJwt
336403''' ;
337404 Clipboard .setData (ClipboardData (text: data));
338- showToast (OpenIoTHubCommonLocalizations .of (context).paste_info);
405+ TDToast .showText (
406+ OpenIoTHubCommonLocalizations .of (context).paste_info,
407+ context: context);
339408 Navigator .of (context).pop ();
340409 },
341410 )
@@ -351,10 +420,15 @@ loginwithtokenmap:
351420 config.description = description;
352421 try {
353422 await SessionApi .createOneSession (config);
354- showToast (OpenIoTHubCommonLocalizations .of (context).add_gateway_success);
423+ TDToast .showText (
424+ OpenIoTHubCommonLocalizations .of (context).add_gateway_success,
425+ context: context);
355426 } catch (exception) {
356- showToast ("${OpenIoTHubCommonLocalizations .of (context ).login_failed }:${exception }" );
427+ TDToast .showText (
428+ "${OpenIoTHubCommonLocalizations .of (context ).login_failed }:${exception }" ,
429+ context: context);
357430 }
431+ // TODO 添加网关主机及网关软件的端口
358432 }
359433
360434 Future <List <DropdownMenuItem <String >>> _listAvailableServer () async {
0 commit comments