Guest User

pimatic-led-light color_action edited

a guest
Oct 27th, 2017
13
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = (env) ->
  2.  
  3.   Promise = env.require 'bluebird'
  4.  
  5.   assert = require 'cassert'
  6.   _ = require 'lodash'
  7.   M = env.matcher
  8.  
  9.   color_schema = require '../color_schema'
  10.  
  11.   class ColorActionHandler extends env.actions.ActionHandler
  12.     constructor: (@provider, @device, @color, @variable, @hex) ->
  13.       @_variableManager = null
  14.  
  15.       if @variable
  16.         @_variableManager = @provider.framework.variableManager
  17.  
  18.     executeAction: (simulate) =>
  19.       getColor = (callback) =>
  20.         if @variable
  21.           if @hex
  22.             callback @variable, simulate
  23.           else
  24.           @_variableManager.evaluateStringExpression([@variable])
  25.             .then (temperature) =>
  26.               temperatureColor = new Color()
  27.               hue = 30 + 240 * (30 - temperature) / 60;
  28.               temperatureColor.hsl(hue, 70, 50)
  29.  
  30.               hexColor = '#'
  31.               hexColor += temperatureColor.rgb().r.toString(16)
  32.               hexColor += temperatureColor.rgb().g.toString(16)
  33.               hexColor += temperatureColor.rgb().b.toString(16)
  34.  
  35.               callback hexColor, simulate
  36.         else
  37.           callback @color, simulate
  38.  
  39.       getColor @setColor
  40.  
  41.     setColor: (color, simulate) =>
  42.       if simulate
  43.         return Promise.resolve(__("would log set color #{color}"))
  44.       else
  45.         @device.setColor color
  46.         return Promise.resolve(__("set color #{color}"))
  47.  
  48.   class ColorActionProvider extends env.actions.ActionProvider
  49.       constructor: (@framework) ->
  50.  
  51.       parseAction: (input, context) =>
  52.         iwyDevices = _(@framework.deviceManager.devices).values().filter(
  53.           (device) => device.hasAction("setColor")
  54.         ).value()
  55.  
  56.         hadPrefix = false
  57.  
  58.         # Try to match the input string with: set ->
  59.         m = M(input, context).match(['set '])
  60.  
  61.         device = null
  62.         color = null
  63.         match = null
  64.         variable = null
  65.  
  66.         # device name -> color
  67.         m.matchDevice iwyDevices, (m, d) ->
  68.           # Already had a match with another device?
  69.           if device? and device.id isnt d.id
  70.             context?.addError(""""#{input.trim()}" is ambiguous.""")
  71.             return
  72.  
  73.           device = d
  74.  
  75.           m.match [' to '], (m) ->
  76.             m.or [
  77.               # rgb hex like #00FF00
  78.               (m) ->
  79.                 # TODO: forward pattern to UI
  80.                 m.match [/(#[a-fA-F\d]{6})(.*)/], (m, s) ->
  81.                   color = s.trim()
  82.                   match = m.getFullMatch()
  83.  
  84.               # color name like red
  85.               (m) -> m.match _.keys(color_schema), (m, s) ->
  86.                   color = color_schema[s]
  87.                   match = m.getFullMatch()
  88.  
  89.               # color by temperature from variable like $weather.temperature = 30
  90.               (m) ->
  91.                 m.match ['temperature based color by variable '], (m) ->
  92.                   m.matchVariable (m, s) ->
  93.                     variable = s
  94.                     match = m.getFullMatch()
  95.               # color by hex variable like $myVar = #FFFFFF
  96.               (m) ->
  97.                 m.match ['hex based color by variable '], (m) ->
  98.                   m.matchVariable (m, s) ->
  99.                     variable = s
  100.                     match = m.getFullMatch()
  101.             ]
  102.  
  103.         if match?
  104.           assert device?
  105.           # either variable or color should be set
  106.           assert variable? ^ color?
  107.           assert typeof match is "string"
  108.           return {
  109.             token: match
  110.             nextInput: input.substring(match.length)
  111.             actionHandler: new ColorActionHandler(@, device, color, variable, hex)
  112.           }
  113.         else
  114.           return null
  115.  
  116.   return ColorActionProvider
RAW Paste Data