Comments (3)
Here is the code:
import 'package:flutter/material.dart';
import 'package:hinox_smarthome/components/card_content.dart';
import 'package:hinox_smarthome/constants.dart';
import 'package:hinox_smarthome/components/reusable_card.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:hinox_smarthome/devices/lighting_device.dart';
import 'package:hinox_smarthome/devices/curtain_device.dart';
class ControlPage extends StatefulWidget {
@OverRide
_ControlPageState createState() => _ControlPageState();
}
class _ControlPageState extends State {
int dimmerValue = 50;
WebSocketChannel channel;
//Light Device 1 variables;
bool light1isOn = false;
bool light2isOn = false;
bool light3isOn = false;
Color light1Color = Colors.grey[800];
Color light2Color = Colors.grey[800];
Color light3Color = Colors.grey[800];
//Curtain Device variables
bool curtainUpActive = false;
bool curtainDownActive = false;
bool curtainLightIsOn = false;
Color curtainUpColor = Colors.grey[800];
Color curtainDownColor = Colors.grey[800];
Color curtainLightColor = Colors.grey[800];
void sendWSData(String dataToSend) {
channel.sink.add(dataToSend);
}
void feedbackActions(String data) {
switch (data) {
case 'aa0100':
light1isOn = false;
light1Color = Colors.grey[300];
break;
case 'aa0101':
light1isOn = true;
light1Color = Colors.orange;
break;
case 'aa0200':
light2isOn = false;
light2Color = Colors.grey[300];
break;
case 'aa0201':
light2isOn = true;
light2Color = Colors.orange;
break;
case 'aa0300':
light3isOn = false;
light3Color = Colors.grey[300];
break;
case 'aa0301':
light3isOn = true;
light3Color = Colors.orange;
break;
default:
break;
}
}
void popUpChoiceAction(String choice) {
switch (choice) {
case settings:
print(settings);
break;
case about:
print(about);
break;
default:
break;
}
}
@OverRide
void initState() {
super.initState();
channel = IOWebSocketChannel.connect('ws://192.168.0.49:81');
channel.stream.listen((data) {
setState(() {
String incomingString = data.toString();
if (incomingString.length >= 18) {
feedbackActions(incomingString.substring(18));
}
});
});
sendWSData('0102');
sendWSData('0202');
sendWSData('0302');
}
@OverRide
void dispose() {
channel.sink.close();
super.dispose();
}
@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hinox Smart Home'),
actions: [
PopupMenuButton(
onSelected: popUpChoiceAction,
itemBuilder: (BuildContext context) {
return menuList.map((String choice) {
return PopupMenuItem(
value: choice,
child: Text(choice),
);
}).toList();
},
),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
LightDevice(
light1Color: light1Color,
light1onPressed: () {
light1isOn ? sendWSData('0100') : sendWSData('0101');
},
light2Color: light2Color,
light2onPressed: () {
light2isOn ? sendWSData('0200') : sendWSData('0201');
},
light3Color: light3Color,
light3onPressed: () {
light3isOn ? sendWSData('0300') : sendWSData('0301');
},
),
CurtainDevice(
curtainUpColor: curtainUpColor,
curtainUpOnPressed: () {},
curtainDownColor: curtainDownColor,
curtainDownOnPressed: () {},
light3Color: curtainLightColor,
light3onPressed: () {}),
Expanded(
child: ReusableCard(
colour: kActiveCardColor,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Scenario Demo',
style: kLabelTextStyle,
),
Row(
children: [
Expanded(
child: ReusableCard(
onPress: () {
setState(() {
//send all on command
sendWSData('0101');
sendWSData('0201');
sendWSData('0301');
});
},
colour: kActiveCardColor,
cardChild: CardContent(
cardIcon: FontAwesomeIcons.solidLightbulb,
cardText: 'All ON',
iconColor: Colors.orange,
),
),
),
Expanded(
child: ReusableCard(
onPress: () {
setState(() {
//send all off
sendWSData('0100');
sendWSData('0200');
sendWSData('0300');
});
},
colour: kActiveCardColor,
cardChild: CardContent(
cardIcon: FontAwesomeIcons.solidLightbulb,
cardText: 'All OFF',
iconColor: Colors.grey,
),
),
),
],
),
],
),
),
),
],
),
);
}
}
from web_socket_channel.
No Such problem exists. I do not know why but after hours of frustration just restarting PC and reinstalling apps worked fine :D
from web_socket_channel.
Please use fenced code blocks when filing issues.
https://help.github.com/en/articles/creating-and-highlighting-code-blocks
closing since the reporter is unable to repro anymore.
from web_socket_channel.
Related Issues (20)
- Auto invoke onDown method to close websocket. HOT 1
- Specifying 'protocols' (Sec-WebSocket-Protocol header) causes connection to close.
- HTML: When the WebSocket is in a 'pending' state, calling channel.sink.close does not work. HOT 2
- Masking is not cryptographically secure HOT 1
- I tried to connect with localhost 8081 port but its connect with diffrent port HOT 2
- FormatException: Unexpected character
- Simplify the web_socket_channel implementation HOT 5
- Message received in wrong order
- Support MSGPACK HOT 1
- Any problems with WebSocketChannel.connect under iOS 17.5 beta (or earlier)? HOT 3
- 🐛 RangeError (code): Invalid value: Not in inclusive range 3000..4999: 1000 after updating to 3.0.0 from 2.4.0 HOT 6
- Missing tags for 2.4.5 and 3.0.0
- Conflict package build_runner versions HOT 1
- Documentation for the 3.x migration HOT 12
- Unexpected Behavior and Premature Closure when Using web_socket_channel to Connect to WebSocket Server HOT 2
- WebSocket clients using Apple private relay can't connect to WebSocket server
- WebSocketChannel has unexpected close code
- WebSocketChannel close code throws exception
- There is not way to get acknowledgement of sent messages. HOT 1
- WebSocket support custom sni host,when use doh HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web_socket_channel.