Code Monkey home page Code Monkey logo

Comments (3)

hiloliddinj avatar hiloliddinj commented on August 17, 2024

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.

hiloliddinj avatar hiloliddinj commented on August 17, 2024

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.

natebosch avatar natebosch commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.