Procházet zdrojové kódy

Make light selection work

Johannes Zellner před 9 roky
rodič
revize
d635d4a58e
5 změnil soubory, kde provedl 104 přidání a 9 odebrání
  1. 14 3
      backend/routes.js
  2. 1 1
      backend/server.js
  3. 5 2
      frontend/index.html
  4. 20 3
      frontend/js/main.js
  5. 64 0
      frontend/views/light.vue

+ 14 - 3
backend/routes.js

@@ -32,11 +32,22 @@ function getGeneric(name) {
 }
 
 function setLight(req, res) {
-    console.log('set light: %s to color %s', req.params.id, req.body.color);
+    console.log('set light: %s', req.params.id, req.body);
 
-    var color = new Color(req.body.color);
+    var state = lightState.create();
 
-    var state = lightState.create().rgb(color.rgb().array());
+    if (req.body.reset) state.reset();
+    if (req.body.colorLoop) state.colorLoop();
+
+    if (typeof req.body.state !== 'undefined') {
+        if (req.body.state) state.on();
+        else state.off();
+    }
+
+    if (req.body.color) {
+        var color = new Color(req.body.color);
+        state.rgb(color.rgb().array());
+    }
 
     api.setLightState(req.params.id, state, function (error, result) {
         if (error) return res.send(500, error);

+ 1 - 1
backend/server.js

@@ -19,7 +19,7 @@ function init(hueConfig) {
     app.get ('/api/config', routes.getConfig);
     app.get ('/api/fullState', routes.getFullState);
     app.get ('/api/lights', routes.getLights);
-    app.post('/api/lights/:id', routes.setLight);
+    app.put ('/api/lights/:id', routes.setLight);
 
     routes.init(hueConfig, function (error) {
         if (error) return console.error(error);

+ 5 - 2
frontend/index.html

@@ -3,6 +3,8 @@
 
     <title>Cloudron HUE</title>
 
+    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
+
     <link rel="stylesheet" href="./css/vue-material.css">
     <link rel="stylesheet" href="./css/material-icons.css">
     <link rel="stylesheet" href="./css/index.css">
@@ -25,7 +27,7 @@
 
                 <md-list-expand>
                     <md-list>
-                        <md-list-item class="md-inset">World</md-list-item>
+                        <md-list-item class="md-inset" v-for="light in lights" @click.native="selectLight(light)">{{ light.name }}</md-list-item>
                     </md-list>
                 </md-list-expand>
             </md-list-item>
@@ -42,7 +44,8 @@
         <h2 class="md-title">Cloudron HUE</h2>
     </md-toolbar>
 
-    <md-button class="md-raised md-accent">Login</md-button>
+    <br/>
+    <light-item :light="selectedLight"></light-item>
 </div>
 
 <script src="./js/bundle.js"></script>

+ 20 - 3
frontend/js/main.js

@@ -4,27 +4,44 @@ var Vue = require('vue'),
     VueMaterial = require('vue-material'),
     VueResource = require('vue-resource');
 
+var Light = require('../views/light.vue');
+
 Vue.use(VueMaterial);
 Vue.use(VueResource);
 
 var App = new Vue({
     el: '#app',
     http: {
-        root: 'https://test.nebulon.info/api',
+        root: '/api',
+    },
+    components: {
+        lightItem: Light
+    },
+    data: {
+        lights: [],
+        selectedLight: {}
     },
     methods: {
-        toggleSidenav() {
+        toggleSidenav: function () {
             this.$refs.leftSidenav.toggle();
         },
-        sidenavOpen() {
+        sidenavOpen: function () {
             console.log('sidenav open');
+        },
+        selectLight: function (light) {
+            this.$data.selectedLight = light;
+            this.$refs.leftSidenav.close();
         }
     },
     mounted: function () {
         console.log('mounted');
 
+        var that = this;
+
         this.$http.get('lights', { responseType: 'text' }).then(function (result) {
             console.log('result', result);
+            that.$data.lights = result.body.lights;
+            that.$data.selectedLight = result.body.lights[3];
         }, function (error) {
             console.log('error', error);
         });

+ 64 - 0
frontend/views/light.vue

@@ -0,0 +1,64 @@
+<style>
+  .red {
+    color: #f00;
+  }
+</style>
+
+<template>
+    <md-layout md-align="center" md-gutter="16">
+        <md-card>
+            <md-card-header>
+                <div class="md-title">{{ light.name }}</div>
+                <div class="md-subhead">{{ light.type }}</div>
+            </md-card-header>
+
+            <md-card-content>
+                <md-switch v-model="stateOn" class="md-warn" @change="toggleOnOff()">State</md-switch>
+            </md-card-content>
+
+            <md-card-actions>
+                <md-button @click.native="reset()">Reset</md-button>
+                <md-button @click.native="setColor('purple')">Purple</md-button>
+                <md-button @click.native="colorLoop()">Color Loop</md-button>
+            </md-card-actions>
+        </md-card>
+    </md-layout>
+</template>
+
+<script>
+
+module.exports = {
+    props: ['light'],
+    data: function () {
+        return {
+            stateOn: false
+        };
+    },
+    http: {
+        root: '/api',
+    },
+    methods: {
+        setProps: function (data) {
+            this.$http.put('lights/' + this.$props.light.id, data).then(function (result) {
+                console.log('result', result);
+            }, function (error) {
+                console.log('error', error);
+            });
+        },
+        toggleOnOff: function () {
+            this.stateOn = !this.stateOn;
+            this.setProps({ state: this.stateOn });
+        },
+        reset: function () {
+            this.setProps({ reset: true });
+        },
+        colorLoop: function () {
+            this.setProps({ effect: 'colorloop' });
+        },
+        setColor: function (color) {
+            this.setProps({ color: color });
+        }
+    }
+}
+
+</script>