This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
pimatic-smartmeter.coffee
95 lines (75 loc) · 2.77 KB
/
pimatic-smartmeter.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# #Plugin pimatic-smartmeter
module.exports = (env) ->
Promise = env.require 'bluebird'
# assert = env.require 'cassert'
class Smartmeter extends env.plugins.Plugin
init: (app, @framework, @config) =>
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("Smartmeterdevice", {
configDef: deviceConfigDef.Smartmeterdevice,
createCallback: (config) => new Smartmeterdevice(config)
})
class Smartmeterdevice extends env.devices.Sensor
attributes:
actualusage:
description: "Actual usage"
type: "number"
unit: " Watt"
acronym: "Actual usage"
activetariff:
description: "Active tariff"
type: "number"
unit: " 1 or 2"
tariff1totalusage:
description: "Tariff 1 total usage(T1)"
type: "number"
unit: " kWh"
tariff2totalusage:
description: "Tariff 2 total usage(T2)"
type: "number"
unit: " kWh"
actualusage: 0.0
activetariff: 1
tariff1totalusage: 0.0
tariff2totalusage: 0.0
constructor: (config) ->
@config = config
@id = @config.id
@name = @config.name
@portName = @config.serialport
@baudRate = @config.baudRate
@dataBits = @config.dataBits
@parity = @config.parity
@stopBits = @config.stopBits
@flowControl = @config.flowControl
super()
if @debug
env.logger.debug ("Smartmeter portName : \"#{@portName}\"")
env.logger.debug ("Smartmeter baudRate : \"#{@baudRate}\"")
env.logger.debug ("Smartmeter dataBits : \"#{@dataBits}\"")
env.logger.debug ("Smartmeter parity : \"#{@parity}\"")
env.logger.debug ("Smartmeter stopBits : \"#{@stopBits}\"")
P1DataStream = require "./p1meterdata"
p1datastream = new P1DataStream({
portName: @portName,
baudRate: @baudRate,
dataBits: @dataBits,
parity: @parity,
stopBits: @stopBits,
flowControl: @flowControl
})
p1datastream.on 'data', (data) =>
@actualusage = Number data.currentUsage
@emit "actualusage", Number @actualusage
@activetariff = Number data.currentTariff
@emit "activetariff", Number @activetariff
@tariff1totalusage = Number data.tariffOneTotalUsage
@emit "tariff1totalusage", Number @tariff1totalusage
@tariff2totalusage = Number data.tariffTwoTotalUsage
@emit "tariff2totalusage", Number @tariff2totalusage
getActualusage: -> Promise.resolve @actualusage
getActivetariff: -> Promise.resolve @activetariff
getTariff1totalusage: -> Promise.resolve @tariff1totalusage
getTariff2totalusage: -> Promise.resolve @tariff2totalusage
plugin = new Smartmeter
return plugin