-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoneKeySwitch.dart
175 lines (163 loc) · 5.09 KB
/
oneKeySwitch.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//oneKeySwitch:https://github.com/iotdevice/esp8266-switch
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:openiothub_grpc_api/proto/mobile/mobile.pb.dart';
import 'package:openiothub_grpc_api/proto/mobile/mobile.pbgrpc.dart';
import '../../../mdnsService/commWidgets/info.dart';
class OneKeySwitchPage extends StatefulWidget {
OneKeySwitchPage({required Key key, required this.device}) : super(key: key);
static final String modelName = "com.iotserv.devices.one-key-switch";
final PortService device;
@override
_OneKeySwitchPageState createState() => _OneKeySwitchPageState();
}
class _OneKeySwitchPageState extends State<OneKeySwitchPage> {
String ledBottonStatus = "off";
@override
void initState() {
super.initState();
_getCurrentStatus();
print("init iot devie List");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("开关控制"),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.settings,
// color: Colors.white,
),
onPressed: () {
_setting();
}),
IconButton(
icon: Icon(
Icons.info,
color: Colors.green,
),
onPressed: () {
_info();
}),
],
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
icon: Icon(Icons.power_settings_new),
color: ledBottonStatus == "on" ? Colors.green : Colors.red,
iconSize: 100.0,
onPressed: () {
_changeSwitchStatus();
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ledBottonStatus == "on" ? Text("已经开启") : Text("已经关闭"),
],
)
]),
);
}
_getCurrentStatus() async {
String url = "http://${widget.device.ip}:${widget.device.port}/status";
http.Response response;
try {
response =
await http.get(Uri.parse(url)).timeout(const Duration(seconds: 2));
print(response.body);
} catch (e) {
print(e.toString());
return;
}
if (response.statusCode == 200) {
setState(() {
ledBottonStatus = jsonDecode(response.body)["led"];
});
}
}
_setting() async {
// TODO 设备设置
TextEditingController _name_controller = TextEditingController.fromValue(
TextEditingValue(text: widget.device.info["name"]!));
return showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text("设置名称:"),
content: SizedBox.expand(
child: ListView(
children: <Widget>[
TextFormField(
controller: _name_controller,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10.0),
labelText: '名称',
),
)
],
),
),
actions: <Widget>[
TextButton(
child: Text("取消"),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text("修改"),
onPressed: () async {
try {
String url =
"http://${widget.device.ip}:${widget.device.port}/rename?name=${_name_controller.text}";
http
.get(Uri.parse(url))
.timeout(const Duration(seconds: 2));
} catch (e) {
print(e.toString());
return;
}
Navigator.of(context).pop();
},
)
]));
}
_info() async {
// TODO 设备信息
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return InfoPage(
portService: widget.device,
key: UniqueKey(),
);
},
),
);
}
_changeSwitchStatus() async {
String url;
url =
"http://${widget.device.ip}:${widget.device.port}/led?status=${ledBottonStatus == "on" ? "off" : "on"}";
http.Response response;
try {
response =
await http.get(Uri.parse(url)).timeout(const Duration(seconds: 2));
print(response.body);
} catch (e) {
print(e.toString());
return;
}
_getCurrentStatus();
}
}