changeset 1438:594160be47f1

device control page now saves new effects and can clear itself Ignore-this: 5166c8f19d3e399e15c1189d3c7e5a70
author drewp@bigasterisk.com
date Sat, 11 Jun 2016 21:03:45 +0000
parents d149a2c2236c
children 951ef08ce4ad
files light9/web/live/index.html light9/web/live/live.coffee
diffstat 2 files changed, 81 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/light9/web/live/index.html	Sat Jun 11 20:41:04 2016 +0000
+++ b/light9/web/live/index.html	Sat Jun 11 21:03:45 2016 +0000
@@ -81,12 +81,24 @@
              color: #9ab8fd;
              text-decoration: initial;
          }
+         #preview {
+             width: 100%;
+         }
+         
         </style>
         <rdfdb-synced-graph graph="{{graph}}"></rdfdb-synced-graph>
 
         <light9-collector-client self="{{client}}"></light9-collector-client>
-        <h1>device control <button on-click="resendAll">resend all</button></h1>
+        <h1>device control <button on-click="resendAll">resend all</button> <button on-click="clearAll">clear all</button></h1>
 
+        <div id="save">
+          <div>
+            effect name: <input type="input" value="{{newEffectName::change}}">
+            <button on-click="saveNewEffect">save new effect</button>
+          </div>
+          <textarea id="preview" value="{{effectPreview}}"></textarea>
+        </div>
+        
         <template is="dom-repeat" items="{{devices}}" as="device">
           <div class="device">
             <h2><a id="mainLabel" href="{{device.uri}}">{{device.label}}</a> (device class <a href="{{device.deviceClass}}">{{device.deviceClass}}</a>)</h2>
--- a/light9/web/live/live.coffee	Sat Jun 11 20:41:04 2016 +0000
+++ b/light9/web/live/live.coffee	Sat Jun 11 21:03:45 2016 +0000
@@ -7,40 +7,99 @@
     device: { type: String }
     deviceAttr: { type: Object }
     max: { type: Number, value: 1 }
-    immediateSlider: { notify: true, observer: 'onChange' }
+    value: { type: Object, notify: true }
+    
+    immediateSlider: { notify: true, observer: 'onSlider' }
     pickedColor: { observer: 'onPickedColor' }
+  observers: [
+    'onChange(value)'
+    ]
   ready: ->
-  onPickedColor: (ev) ->
-    @onChange ev.target.value
-  goWhite: -> @onChange("#ffffff")
-  goBlack: -> @onChange("#000000")
-  onChange: (lev) ->
-    @lastSent = [[@device, @deviceAttr.uri, lev]]
+    
+  onPickedColor: (ev) -> @value = ev.target.value
+  onSlider: -> @value = @immediateSlider
+  goWhite: -> @value = "#ffffff"
+  goBlack: -> @value = "#000000"
+  
+  onChange: (value) ->
+    @lastSent = [[@device, @deviceAttr.uri, value]]
     @resend()
   resend: ->
     @client.send(@lastSent)
+    window.gather(@lastSent)
+  clear: ->
+    if @deviceAttr.useColor
+      @value = '#000000'
+    else
+      @value = 0
+      
   
 Polymer
   is: "light9-live-controls"
   properties:
     graph: { type: Object, notify: true }
     devices: { type: Array, notify: true }
+    currentSettings: { type: Object, notify: true } # dev+attr: [dev, attr, value]
+    effectPreview: { type: String, notify: true }
+    newEffectName: { type: String, notify: true }
   observers: [
     'onGraph(graph)'
     ]
+  ready: ->
+    @currentSettings = {}
+    @effectPreview = JSON.stringify({})
+    window.gather = (sent) =>
+      [dev, devAttr, value] = sent[0]
+      return if value == 0
+      @currentSettings[dev + " " + devAttr] = [dev, devAttr, value]
+      @effectPreview = JSON.stringify(v for k,v of @currentSettings)
+  saveNewEffect: ->
+    return if not @newEffectName.length
+
+    U = (x) -> @graph.Uri(x)
+
+    effectUri = U(":effect") + "/#{@newEffectName}"
+    ctx = U('http://light9.bigasterisk.com/show/dance2016/effect')
+    quad = (s, p, o) => {subject: s, predicate: p, object: o, graph: ctx}
+
+    addQuads = [
+      quad(effectUri, U('rdf:type'), U(':Effect'))
+      quad(effectUri, U('rdfs:label'), @graph.Literal(@newEffectName))
+      quad(effectUri, U(':publishAttr'), U(':strength'))
+      ]
+    settings = @graph.nextNumberedResources(effectUri + '_set', Object.keys(@currentSettings).length)
+    for _, row of @currentSettings
+      setting = settings.shift()
+      addQuads.push(quad(effectUri, U(':setting'), setting))
+      addQuads.push(quad(setting, U(':device'), row[0]))
+      addQuads.push(quad(setting, U(':deviceAttr'), row[1]))
+      scaledAttributeTypes = [U(':color'), U(':brightness'), U(':uv')]
+      value = if typeof(row[2]) == 'number'
+          @graph.LiteralRoundedFloat(row[2])
+        else
+          @graph.Literal(row[2])
+      settingType = if row[1] in scaledAttributeTypes then U(':scaledValue') else U(':value')
+      addQuads.push(quad(setting, settingType, value))
+      
+    patch = {addQuads: addQuads, delQuads: []}
+    log('save', patch)
+    @graph.applyAndSendPatch(patch)
+
   onGraph: ->
     @graph.runHandler(@update.bind(@))
   resendAll: ->
     for llc in @getElementsByTagName("light9-live-control")
       llc.resend()
+  clearAll: ->
+    for llc in @getElementsByTagName("light9-live-control")
+      llc.clear()
+    
   update: ->
     U = (x) -> @graph.Uri(x)
 
     @set('devices', [])
     for dc in _.sortBy(@graph.subjects(U('rdf:type'), U(':DeviceClass')))
-      log('dc', dc)      
       for dev in _.sortBy(@graph.subjects(U('rdf:type'), dc))
-        log('dev', dev)
         row = {uri: dev, label: (try
             @graph.stringValue(dev, U('rdfs:label'))
           catch