forked from mwittig/pimatic-unipi-evok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unipi-helper.coffee
56 lines (46 loc) · 1.6 KB
/
unipi-helper.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
module.exports = (env) ->
Promise = env.require 'bluebird'
url = require 'url'
_ = env.require 'lodash'
# UniPi helper functions
uniPiHelper =
createDeviceUrl: (baseUrl, deviceType, circuit) ->
urlObject = url.parse baseUrl, false, true
urlObject.pathname = "rest/" + deviceType + "/" + circuit
return url.format urlObject
getErrorResult: (data) ->
errorMessage = uniPiHelper.getPath data, 'errors.__all__'
unless _.isString errorMessage
errorMessage = "failed"
return new Error errorMessage
normalize: (value, lowerRange, upperRange) ->
if upperRange
return Math.min (Math.max value, lowerRange), upperRange
else
return Math.max value lowerRange
# helper function to get the object path as older versions of lodash do not support this
getPath: (obj, path) ->
return undefined if not _.isObject obj or not _.isString path
keys = path.split '.'
for key in keys
if not _.isObject(obj) or not obj.hasOwnProperty(key)
return undefined
obj = obj[key]
return obj
parseGetResponse: (result) ->
try
json = JSON.parse result.data
return Promise.resolve json
catch err
return Promise.reject err
parsePostResponse: (result) ->
uniPiHelper.parseGetResponse(result).then((json) =>
if json.success
return Promise.resolve json
else
uniPiError = uniPiHelper.getErrorResult json
return Promise.reject uniPiError
).catch((error) =>
return Promise.reject error
)
return uniPiHelper