Skip to content

Commit

Permalink
Merge pull request #29 from philip1986/development_xento
Browse files Browse the repository at this point in the history
Development xento
  • Loading branch information
philip1986 committed Oct 5, 2015
2 parents 6bacc2b + b89785c commit 0076d2f
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 66 deletions.
43 changes: 20 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Currently supported:

## Installation

To install the plugin on a Debian or Raspbian system libudev-dev must be installed.
To install the plugin on a Debian or Raspbian system libudev-dev must be installed.

sudo apt-get install libudev-dev

Expand Down Expand Up @@ -41,30 +41,27 @@ To install the plugin on a Debian or Raspbian system libudev-dev must be install

### For MilightRF24

Pluginconfig:
```
{
"plugin": "led-light",
"MilightRF24Port": "/dev/ttyUSB1"
}
```

Devices:
```
"zones": [
{
"addr": "5927",
"zone": 0,
"send": true,
"receive": true
},
{
"addr": "485D",
"zone": 0,
"send": true,
"receive": true
}
]
"id": 'some_id'
"name": 'some_name'
"class": 'MilightRF24'
"port": '/dev/ttyUSB1'
"zones": [
{
"addr": "5927",
"zone": 0,
"send": true,
"receive": true
},
{
"addr": "485D",
"zone": 0,
"send": true,
"receive": true
}
]
}
```
You will get your addr when you just add the parameter MilightRF24Port to your config and switch to the debug output in pimatic and change some settings with your remote.

Expand Down
3 changes: 3 additions & 0 deletions device-config-schema.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module.exports = {
addr:
description: "Address of light device"
type: "string"
port:
description: "USB port where the gateway is attached"
type: "string"
zone:
description: "Zone [0 - 4], 0 = switches all zones"
type: "number"
Expand Down
107 changes: 79 additions & 28 deletions devices/milightRF24.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@ module.exports = (env) ->
Color = require 'color'
nodeMilightRF24 = require 'node-milight-rf24'
Buttons = nodeMilightRF24.RGBWButtons
nodeMilightRF24 = nodeMilightRF24.MilightRF24Controller
NodeMilightRF24 = nodeMilightRF24.MilightRF24Controller
BaseLedLight = require('./base')(env)
events = require('events')

class MilightRF24
# Handles the connection to the arduino (receives and sends messages)
class MilightRF24 extends events.EventEmitter
# singelton gatway connetion
@connectToGateway: (config) ->
unless MilightRF24.instance
MilightRF24.instance = new MilightRF24 config
return MilightRF24.instance

constructor: (@config) ->
@gateway = new nodeMilightRF24({port: @config.MilightRF24Port})
env.logger.debug "Opening "+@config.MilightRF24Port
self = @
@gateway = new NodeMilightRF24
port: @config.port

env.logger.debug "Opening #{@config.port}"
@gateway.open()

events.EventEmitter.call(this);

@gateway.on("Received", (data) ->
env.logger.debug data

self.emit("ReceivedData", data);
)

getGateway: ->
Expand All @@ -27,10 +42,14 @@ module.exports = (env) ->
env.logger.debug "Sending Color. Addr: #{id} Zone: #{zone} Red: #{r} Green: #{g} Blue: #{b}"
@gateway.setColor(id, zone, r,g,b)

@_loop(id, zone, Buttons.ColorFader, false, 0, 0, r,g,b)

setBrightness: (id, zone, brightness) ->
env.logger.debug "Sending Brightness. Addr:#{id} Zone:#{zone} Brightness:#{brightness}"
@gateway.setBrightness(id, zone, brightness)

@_loop(id, zone, Buttons.BrightnessFader, false, 0, brightness, 0,0,0)

setWhite: (id, zone) ->
env.logger.debug "Sending Whitemode. Addr:#{id} Zone:#{zone}"
switch zone
Expand All @@ -47,6 +66,8 @@ module.exports = (env) ->

@gateway.sendButton(id, zone, button, true)

@_loop(id, zone, button, true, 0, 0, 0,0,0)

turnOn: (id, zone) ->
env.logger.debug "Sending On. Addr:#{id} Zone:#{zone}"
switch zone
Expand All @@ -63,6 +84,8 @@ module.exports = (env) ->

@gateway.sendButton(id, zone, button, false)

@_loop(id, zone, button, false, 0, 0, 0,0,0)

turnOff: (id, zone) ->
env.logger.debug "Sending Off. Addr:#{id} Zone:#{zone}"
switch zone
Expand All @@ -78,7 +101,28 @@ module.exports = (env) ->
button = Buttons.Group4Off

@gateway.sendButton(id, zone, button, false)


@_loop(id, zone, button, false, 0, 0, 0,0,0)

# loop for changes to zone 0 to be reflected by all other zones which have the same id
_loop: (id, zone, button, longPress, discoMode, brightness, r, g, b) ->
dataObj =
raw: "loop",
id: id,
zone: zone,
button: button,
longPress: longPress,
discoMode: discoMode,
brightness: brightness,
color:
r: r,
g: g,
b: b

@.emit("ReceivedData", dataObj);

# registers for messages from the main class and checks if incoming messages are addressed at the registered ids and zone combination
# sends changes from the gui to the main class, so that they are send to the arduino
class MilightRF24Zone extends BaseLedLight

constructor: (@config, lastState, MilightRF24Gateway) ->
Expand All @@ -95,22 +139,29 @@ module.exports = (env) ->
super(initState)
if @power then @turnOn() else @turnOff()

@gateway.getGateway().on('Received', (data) ->
# register for incoming messages
@gateway.on('ReceivedData', (data) ->
self.zones.forEach (z) ->

# check if this zone listens on the current zone from config
unless z.receive is false
if z.addr is data.id and z.zone is data.zone
env.logger.debug data
switch data.button
when Buttons.AllOn, Buttons.Group1On, Buttons.Group2On, Buttons.Group3On, Buttons.Group4On
self.turnOn(false)
when Buttons.AllOff, Buttons.Group1Off, Buttons.Group2Off, Buttons.Group3Off, Buttons.Group4Off
self.turnOff(false)
when (Buttons.AllOn or Buttons.Group1On or Buttons.Group2On or Buttons.Group3On or Buttons.Group4On) and data.longPress is true
self.setWhite(false)
when Buttons.ColorFader or Buttons.FaderReleased
self.setColor("#"+self._num2Hex(data.color.r)+self._num2Hex(data.color.g)+self._num2Hex(data.color.b), false)
when Buttons.BrightnessFader or Buttons.FaderReleased
self.setBrightness(data.brightness, false)
if z.addr is data.id
if data.button is Buttons.AllOff or (data.button is Buttons.Group1Off and z.zone is 1) or (data.button is Buttons.Group2Off and z.zone is 2) or (data.button is Buttons.Group3Off and z.zone is 3) or (data.button is Buttons.Group4Off and z.zone is 4)
self.turnOff(false)

if (z.zone is data.zone or data.zone is 0) and data.longPress is true and (Buttons.AllOn or Buttons.Group1On or Buttons.Group2On or Buttons.Group3On or Buttons.Group4On)
self.setWhite(false)

if z.zone is data.zone or data.zone is 0
switch data.button
when Buttons.AllOn, Buttons.Group1On, Buttons.Group2On, Buttons.Group3On, Buttons.Group4On
self.turnOn(false)
when Buttons.AllOff
self.turnOff(false)
when Buttons.ColorFader or Buttons.FaderReleased
self.setColor("#"+self._num2Hex(data.color.r)+self._num2Hex(data.color.g)+self._num2Hex(data.color.b), false)
when Buttons.BrightnessFader or Buttons.FaderReleased
self.setBrightness(data.brightness, false)
)

_num2Hex: (s) ->
Expand All @@ -125,21 +176,21 @@ module.exports = (env) ->

turnOn: (send) ->
self = @

env.logger.debug "Turn on"

@_updateState power: true

@zones.forEach (z) ->
unless z.send is false or send is false
self.gateway.turnOn(z.addr, z.zone)
if self.mode
color = Color(self.color).rgb()
self.gateway.setColor(z.addr, z.zone, color.r, color.g, color.b, true)
else
self.gateway.setWhite(z.addr, z.zone)

self.gateway.setBrightness(z.addr, z.zone, self.brightness)

unless z.zone is 0
if self.mode
color = Color(self.color).rgb()
self.gateway.setColor(z.addr, z.zone, color.r, color.g, color.b, true)
else
self.gateway.setWhite(z.addr, z.zone)

self.gateway.setBrightness(z.addr, z.zone, self.brightness)

Promise.resolve()

Expand Down
8 changes: 2 additions & 6 deletions led-light-schema.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
module.exports = {
title: "Plugin config options"
type: "object"
properties:
MilightRF24Port:
description: "Port of arduino with openmili sketch"
type: "string"
default: ""
}
properties: {}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"color": "^0.8.0",
"iwy_master": "0.2.2",
"node-milight-promise": ">=0.0.2",
"node-milight-rf24": ">=0.0.1",
"node-milight-rf24": ">=0.1.1",
"lodash": "^3.5.0",
"blinkstick": "1.1.1"
},
Expand Down
11 changes: 4 additions & 7 deletions pimatic-led-light.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ module.exports = (env) ->

init: (app, @framework, @config) =>
deviceConfigDef = require('./device-config-schema.coffee')

if @config.MilightRF24Port != ""
MilightRF24 = new MilightRF24(@config)

@framework.deviceManager.registerDeviceClass 'IwyMaster',
configDef: deviceConfigDef.LedLight
Expand All @@ -32,10 +29,10 @@ module.exports = (env) ->
configDef: deviceConfigDef.Milight
createCallback: (config, lastState) -> return new Milight(config, lastState)

if @config.MilightRF24Port != ""
@framework.deviceManager.registerDeviceClass 'MilightRF24',
configDef: deviceConfigDef.MilightRF24
createCallback: (config, lastState) -> return MilightRF24.getDevice(config, lastState)
@framework.deviceManager.registerDeviceClass 'MilightRF24',
configDef: deviceConfigDef.MilightRF24
createCallback: (config, lastState) ->
return MilightRF24.connectToGateway(config).getDevice(config, lastState)

unless process.env.NODE_ENV is 'travis-test'
@framework.deviceManager.registerDeviceClass 'Blinkstick',
Expand Down
1 change: 1 addition & 0 deletions test/app_stub/env.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exports.env =
plugins:
Plugin: class Plugin
logger:
debug: sinon.stub()
info: sinon.stub()
warn: sinon.stub()
error: sinon.stub()
1 change: 0 additions & 1 deletion test/app_stub/framework.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ exports.loadPluginWithEnvAndConfig = (env, deviceClass, deviceConfig) ->

plugin.init null, framework,
plugin: 'led-light'
MilightRF24Port: '' # fix MilightRF24 integration in pimatic-led-light.coffee

return framework.device
25 changes: 25 additions & 0 deletions test/driver_stubs/milightRF24.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
sinon = require 'sinon'
{ EventEmitter } = require 'events'
MilightRF24 = require 'node-milight-rf24'

# replace whole Controller class
MilightRF24.MilightRF24Controller = class FakeMilightRF24Controller extends EventEmitter
constructor: (@config) ->
open: ->
setColor: ->
setBrightness: ->
sendButton: ->

class DriverStub
@open = sinon.stub MilightRF24.MilightRF24Controller.prototype, 'open'
@setColor = sinon.stub MilightRF24.MilightRF24Controller.prototype, 'setColor'
@setBrightness = sinon.stub MilightRF24.MilightRF24Controller.prototype, 'setBrightness'
@sendButton = sinon.stub MilightRF24.MilightRF24Controller.prototype, 'sendButton'

@reset: ->
@open.reset()
@setColor.reset()
@setBrightness.reset()
@sendButton.reset()

exports.DriverStub = DriverStub
Loading

0 comments on commit 0076d2f

Please sign in to comment.