Advertisement
Guest User

pimatic-led-light color_action original

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