ソースを参照

make color selection work

Johannes Zellner 9 年 前
親
コミット
0d1577fd48
4 ファイル変更、74 行追加、24 行削除
  1. 13 8
      backend/routes.js
  2. 21 10
      frontend/js/main.js
  3. 39 6
      frontend/views/light.vue
  4. 1 0
      package.json

+ 13 - 8
backend/routes.js

@@ -11,8 +11,7 @@ module.exports = {
 
 
 var hue = require('node-hue-api'),
 var hue = require('node-hue-api'),
     HueApi = hue.HueApi,
     HueApi = hue.HueApi,
-    lightState = hue.lightState,
-    Color = require('color');
+    lightState = hue.lightState;
 
 
 var api = null;
 var api = null;
 
 
@@ -33,9 +32,12 @@ function getGeneric(name) {
 }
 }
 
 
 function getLight(req, res) {
 function getLight(req, res) {
-    api.lightStatus(req.params.id, function (error, result) {
+    api.lightStatusWithRGB(req.params.id, function (error, result) {
         if (error) return res.send(500, 'Unable to get state of light ' + req.params.id);
         if (error) return res.send(500, 'Unable to get state of light ' + req.params.id);
 
 
+        // id is not part of the light status api
+        result.id = req.params.id;
+
         res.send(200, result);
         res.send(200, result);
     });
     });
 }
 }
@@ -48,14 +50,17 @@ function setLight(req, res) {
     if (req.body.reset) state.reset();
     if (req.body.reset) state.reset();
     if (req.body.colorLoop) state.colorLoop();
     if (req.body.colorLoop) state.colorLoop();
 
 
-    if (typeof req.body.state !== 'undefined') {
-        if (req.body.state) state.on();
+    if (typeof req.body.on !== 'undefined') {
+        if (req.body.on) state.on();
         else state.off();
         else state.off();
     }
     }
 
 
-    if (req.body.color) {
-        var color = new Color(req.body.color);
-        state.rgb(color.rgb().array());
+    if (req.body.rgb) {
+        state.rgb(req.body.rgb);
+    }
+
+    if (req.body.bri) {
+        state.bri(req.body.bri);
     }
     }
 
 
     api.setLightState(req.params.id, state, function (error, result) {
     api.setLightState(req.params.id, state, function (error, result) {

+ 21 - 10
frontend/js/main.js

@@ -1,4 +1,5 @@
-var util = require('util');
+var async = require('async'),
+    util = require('util');
 
 
 var Vue = require('vue'),
 var Vue = require('vue'),
     VueMaterial = require('vue-material'),
     VueMaterial = require('vue-material'),
@@ -28,15 +29,26 @@ var methods = {
         this.$http.get('lights').then(function (result) {
         this.$http.get('lights').then(function (result) {
             console.log('result', result);
             console.log('result', result);
 
 
-            result.body.lights.forEach(function (light) {
-                var tmp = that.$data.lights.find(function (l) { return light.id === l.id; });
-                if (!tmp) return that.$data.lights.push(light);
+            async.each(result.body.lights, function (light, callback) {
+                that.$http.get('lights/' + light.id).then(function (result) {
+                    var light = result.body;
 
 
-                tmp = light;
-            });
+                    var tmp = that.$data.lights.find(function (l) { return light.id === l.id; });
+                    if (!tmp) {
+                        that.$data.lights.push(light);
+                    } else {
+                        tmp = light;
+                    }
 
 
-            // init a light selection
-            if (!that.$data.selectedLight.id && that.$data.lights.length > 0) that.$data.selectedLight = result.body.lights[0];
+                    callback();
+                }, function (error) {
+                    console.log('error', error);
+                    callback();
+                });
+            }, function () {
+                // init a light selection
+                if (!that.$data.selectedLight.id && that.$data.lights.length > 0) that.$data.selectedLight = that.$data.lights[0];
+            });
         }, function (error) {
         }, function (error) {
             console.log('error', error);
             console.log('error', error);
         });
         });
@@ -45,8 +57,7 @@ var methods = {
         var that = this;
         var that = this;
 
 
         this.$http.put('lights/' + light.id, light.state).then(function (result) {
         this.$http.put('lights/' + light.id, light.state).then(function (result) {
-            console.log('result', result);
-            that.refreshLights();
+            // console.log('result', result);
         }, function (error) {
         }, function (error) {
             console.log('error', error);
             console.log('error', error);
         });
         });

+ 39 - 6
frontend/views/light.vue

@@ -37,13 +37,19 @@
 
 
 <script>
 <script>
 
 
-var VueColor = require('vue-color');
+var Color = require('color');
+
+var Vue = require('vue'),
+    VueColor = require('vue-color');
 
 
 module.exports = {
 module.exports = {
     props: ['light'],
     props: ['light'],
     data: function () {
     data: function () {
         return {
         return {
-            colors: {}
+            colors: {
+                hex: '#00ff00',
+                a: 0
+            }
         };
         };
     },
     },
     http: {
     http: {
@@ -57,23 +63,50 @@ module.exports = {
             this.setProps({ reset: true });
             this.setProps({ reset: true });
         },
         },
         colorLoop: function () {
         colorLoop: function () {
-            this.setProps({ effect: 'colorloop' });
+            console.error('Not implemented')
         },
         },
         setColor: function (color) {
         setColor: function (color) {
-            this.setProps({ color: color });
+            Vue.set(this.$props.light.state, 'rgb', Color(color).rgb().array());
+        },
+        setRgb: function (r, g, b) {
+            Vue.set(this.$props.light.state, 'rgb', [r, g, b]);
+        },
+        setBrightness: function (brightness) {
+            Vue.set(this.$props.light.state, 'bri', brightness);
         }
         }
     },
     },
     watch: {
     watch: {
         light: {
         light: {
             handler: function (newVal, oldVal) {
             handler: function (newVal, oldVal) {
-                // this is just setting a new light
-                if (newVal.id !== oldVal.id) return;
+                var that = this;
+
+                // this is setting a new light
+                if (newVal.id !== oldVal.id) {
+                    console.log(newVal.state)
+
+                    if (!newVal.state.rgb) return;
+
+                    console.log(this.$data, Color.rgb(newVal.state.rgb).hex())
+                    Vue.nextTick(function () {
+                        that.$data.colors = {
+                            hex: Color.rgb(newVal.state.rgb).hex(),
+                            a: 255. / newVal.state.bri
+                        };
+                    });
+                    return;
+                }
 
 
                 console.log('light changed, commit changes', newVal, oldVal);
                 console.log('light changed, commit changes', newVal, oldVal);
 
 
                 this.$root.setLightProperties(this.$props.light);
                 this.$root.setLightProperties(this.$props.light);
             },
             },
             deep: true
             deep: true
+        },
+        colors: {
+            handler: function (newVal, oldVal) {
+                this.setRgb(newVal.rgba.r, newVal.rgba.g, newVal.rgba.b);
+                this.setBrightness(newVal.rgba.a * 255);
+            }
         }
         }
     }
     }
 }
 }

+ 1 - 0
package.json

@@ -9,6 +9,7 @@
   "author": "",
   "author": "",
   "license": "ISC",
   "license": "ISC",
   "dependencies": {
   "dependencies": {
+    "async": "^2.4.1",
     "body-parser": "^1.17.2",
     "body-parser": "^1.17.2",
     "color": "^1.0.3",
     "color": "^1.0.3",
     "cors": "^2.8.3",
     "cors": "^2.8.3",