-
Notifications
You must be signed in to change notification settings - Fork 89.9k
/
smart-energy-service.groovy
1388 lines (1164 loc) · 53 KB
/
smart-energy-service.groovy
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ProtoType Smart Energy Service
*
* Copyright 2015 hyeon seok yang
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
definition(
name: "Smart Energy Service",
namespace: "Encored Technologies",
author: "hyeon seok yang",
description: "With visible realtime energy usage status, have good energy habits and enrich your life\r\n",
category: "SmartThings Labs",
iconUrl: "https://s3-ap-northeast-1.amazonaws.com/smartthings-images/appicon_enertalk%401.png",
iconX2Url: "https://s3-ap-northeast-1.amazonaws.com/smartthings-images/appicon_enertalk%402x",
iconX3Url: "https://s3-ap-northeast-1.amazonaws.com/smartthings-images/appicon_enertalk%403x",
oauth: true)
{
appSetting "clientId"
appSetting "clientSecret"
appSetting "callback"
}
preferences {
page(name: "checkAccessToken")
}
cards {
card(name: "Encored Energy Service", type: "html", action: "getHtml", whitelist: whiteList()) {}
}
/* This list contains, that url need to be allowed in Smart Energy Service.*/
def whiteList() {
[
"code.jquery.com",
"ajax.googleapis.com",
"fonts.googleapis.com",
"code.highcharts.com",
"enertalk-card.encoredtech.com",
"s3-ap-northeast-1.amazonaws.com",
"s3.amazonaws.com",
"ui-hub.encoredtech.com",
"enertalk-auth.encoredtech.com",
"api.encoredtech.com",
"cdnjs.cloudflare.com",
"encoredtech.com",
"itunes.apple.com"
]
}
/* url endpoints */
mappings {
path("/requestCode") { action: [ GET: "requestCode" ] }
path("/receiveToken") { action: [ GET: "receiveToken"] }
path("/getHtml") { action: [GET: "getHtml"] }
path("/consoleLog") { action: [POST: "consoleLog"]}
path("/getInitialData") { action: [GET: "getInitialData"]}
path("/getEncoredPush") { action: [POST: "getEncoredPush"]}
}
/* This method does two things depends on the existence of Encored access token. :
* 1. If Encored access token does not exits, it starts the process of getting access token.
* 2. If Encored access token does exist, it will show a list of configurations, that user need to define values.
*/
def checkAccessToken() {
log.debug "Staring the installation"
/* Choose the level */
atomicState.env_mode ="prod"
def lang = clientLocale?.language
/* getting language settings of user's device. */
if ("${lang}" == "ko") {
atomicState.language = "ko"
} else {
atomicState.language = "en"
}
/* create tanslation for descriptive and informative strings that can be seen by users. */
if (!state.languageString) {
createLocaleStrings()
}
if (!atomicState.encoredAccessToken) { /*check if Encored access token does exist.*/
log.debug "Encored Access Token does not exist."
if (!state.accessToken) { /*if smartThings' access token does not exitst*/
log.debug "SmartThings Access Token does not exist."
createAccessToken() /*request and get access token from smartThings*/
/* re-create strings to make sure it's been initialized. */
//createLocaleStrings()
}
def redirectUrl = buildRedirectUrl("requestCode") /* build a redirect url with endpoint "requestCode"*/
/* These lines will start the OAuth process.\n*/
log.debug "Start OAuth request."
return dynamicPage(name: "checkAccessToken", nextPage:null, uninstall: true, install:false) {
section{
paragraph state.languageString."${atomicState.language}".desc1
href(title: state.languageString."${atomicState.language}".main,
description: state.languageString."${atomicState.language}".desc2,
required: true,
style:"embedded",
url: redirectUrl)
}
}
} else {
/* This part will load the configuration for this application */
return dynamicPage(name:"checkAccessToken",install:true, uninstall : true) {
section(title:state.languageString."${atomicState.language}".title6) {
/* A push alarm for this application */
input(
type: "boolean",
name: "notification",
title: state.languageString."${atomicState.language}".title1,
required: false,
default: true,
multiple: false
)
/* A plan that user need to decide */
input(
type: "number",
name: "energyPlan",
title: state.languageString."${atomicState.language}".title2,
description : state.languageString."${atomicState.language}".subTitle1,
defaultValue: state.languageString.energyPlan,
range: "1130..*",
submitOnChange: true,
required: true,
multiple: false
)
/* A displaying unit that user need to decide */
input(
type: "enum",
name: "displayUnit",
title: state.languageString."${atomicState.language}".title3,
defaultValue : state.languageString."${atomicState.language}".defaultValues.default1,
required: true,
multiple: false,
options: state.languageString."${atomicState.language}".displayUnits
)
/* A metering date that user should know */
input(
type: "enum",
name: "meteringDate",
title: state.languageString."${atomicState.language}".title4,
defaultValue: state.languageString."${atomicState.language}".defaultValues.default2,
required: true,
multiple: false,
options: state.languageString."${atomicState.language}".meteringDays
)
/* A contract type that user should know */
input(
type: "enum",
name: "contractType",
title: state.languageString."${atomicState.language}".title5,
defaultValue: state.languageString."${atomicState.language}".defaultValues.default3,
required: true,
multiple: false,
options: state.languageString."${atomicState.language}".contractTypes)
}
}
}
}
def requestCode(){
log.debug "In state of sending a request to Encored for OAuth code.\n"
/* Make a parameter to request Encored for a OAuth code. */
def oauthParams =
[
response_type: "code",
scope: "remote",
client_id: "${appSettings.clientId}",
app_version: "web",
redirect_uri: buildRedirectUrl("receiveToken")
]
/* Request Encored a code. */
redirect location: "https://enertalk-auth.encoredtech.com/authorization?${toQueryString(oauthParams)}"
}
def receiveToken(){
log.debug "Request Encored to swap code with Encored Aceess Token"
/* Making a parameter to swap code with a token */
def authorization = "Basic " + "${appSettings.clientId}:${appSettings.clientSecret}".bytes.encodeBase64()
def uri = "https://enertalk-auth.encoredtech.com/token"
def header = [Authorization: authorization, contentType: "application/json"]
def body = [grant_type: "authorization_code", code: params.code]
log.debug "Swap code with a token"
def encoredTokenParams = makePostParams(uri, header, body)
log.debug "API call to Encored to swap code with a token"
def encoredTokens = getHttpPostJson(encoredTokenParams)
/* make a page to show people if the REST was successful or not. */
if (encoredTokens) {
log.debug "Got Encored OAuth token\n"
atomicState.encoredRefreshToken = encoredTokens.refresh_token
atomicState.encoredAccessToken = encoredTokens.access_token
success()
} else {
log.debug "Could not get Encored OAuth token\n"
fail()
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
/* Make sure uuid is there. */
getUUID()
/* Check uuid and if it does not exist then don't update.*/
if (!atomicState.notPaired) {
def theDay = 1
for(def i=1; i < 28; i++) {
/* set user choosen option to apropriate value. */
if (atomicState.language == "en") {
if ("${i}st day of the month" == settings.meteringDate ||
"${i}nd day of the month" == settings.meteringDate ||
"${i}rd day of the month" == settings.meteringDate ||
"${i}th day of the month" == settings.meteringDate) {
theDay = i
i = 28
} else if ("Rest of the month" == settings.meteringDate) {
theDay = 27
i = 28
}
} else {
if (settings.meteringDate == "매월 ${i}일") {
theDay = i
i = 28
} else if ("말일" == settings.meteringDate) {
theDay = 27
i = 28
}
}
}
/* Set choosen contract to apropriate variable. */
def contract = 1
if (settings.contractType == "High voltage" || settings.contractType == "주택용 고압") {
contract = 2
if (settings.energyPlan < 460) {
settings.energyPlan = 490
}
} else {
if (settings.energyPlan < 1130) {
settings.energyPlan = 1130
}
}
/* convert bill to milliwatts */
def changeToUsageParam = makeGetParams("${state.domains."${atomicState.env_mode}"}/1.2/devices/${atomicState.uuid}/bill/expectedUsage?bill=${settings.energyPlan}",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
def energyPlanUsage = getHttpGetJson(changeToUsageParam, 'CheckEnergyPlanUsage')
def epUsage = 0
if (energyPlanUsage) {
epUsage = energyPlanUsage.usage
}
/* update the the information depends on the option choosen */
def configurationParam = makePostParams("${state.domains."${atomicState.env_mode}"}/1.2/me",
[Authorization : "Bearer ${atomicState.encoredAccessToken}"],
[contractType : contract,
meteringDay : theDay,
maxLimitUsage : epUsage])
getHttpPutJson(configurationParam)
}
}
def initialize() {
log.debug "Initializing Application"
def EATValidation = checkEncoreAccessTokenValidation()
/* if token exist get user's device id, uuid */
if (EATValidation) {
getUUID()
if (atomicState.uuid) {
def pushParams = makePostParams("${state.domains."${atomicState.env_mode}"}/1.2/devices/${atomicState.uuid}/events/push",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"],
[type: "REST", regId:"${state.accessToken}__${app.id}"])
getHttpPostJson(pushParams)
}
} else {
log.warning "Ecored Access Token did not get refreshed!"
}
/* add device Type Handler */
atomicState.dni = "EncoredDTH01"
def d = getChildDevice(atomicState.dni)
if(!d) {
log.debug "Creating Device Type Handler."
d = addChildDevice("Encored Technologies", "EnerTalk Energy Meter", atomicState.dni, null, [name:"EnerTalk Energy Meter", label:name])
} else {
log.debug "Device already created"
}
setSummary()
}
def setSummary() {
log.debug "in setSummary"
def text = "Successfully installed."
sendEvent(linkText:count.toString(), descriptionText: app.label,
eventType:"SOLUTION_SUMMARY",
name: "summary",
value: text,
data: [["icon":"indicator-dot-gray","iconColor":"#878787","value":text]],
displayed: false)
}
// TODO: implement event handlers
/* Check the validation of Encored Access Token (EAT)
* If it's not valid try refresh Access Token.
* If the token gets refreshed, it will refresh the value of Encored Access Token
* If it doesn't get refreshed, then it returns null
*/
private checkEncoreAccessTokenValidation() {
/* make a parameter to check the validation of Encored access token */
def verifyParam = makeGetParams("https://enertalk-auth.encoredtech.com/verify",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
/* check the validation */
def verified = getHttpGetJson(verifyParam, 'verifyToken')
log.debug "verified : ${verified}"
/* if Encored Access Token need to be renewed. */
if (!verified) {
try {
refreshAuthToken()
/* Recheck the renewed Encored access token. */
verifyParam.headers = [Authorization: "Bearer ${atomicState.encoredAccessToken}"]
verified = getHttpGetJson(verifyParam, 'CheckRefresh')
} catch (groovyx.net.http.HttpResponseException e) {
/* If refreshing token raises an error */
log.warn "Refresh Token Error : ${e}"
}
}
return verified
}
/* Get device UUID, if it does not exist, return false. true otherwise.*/
private getUUID() {
atomicState.uuid = null
atomicState.notPaired = true
/* Make a parameter to get device id (uuid)*/
def uuidParams = makeGetParams( "https://enertalk-auth.encoredtech.com/uuid",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
def deviceUUID = getHttpGetJson(uuidParams, 'UUID')
log.debug "device uuid is : ${deviceUUID}"
if (!deviceUUID) {
return false
}
log.debug "got here even tho"
atomicState.uuid = deviceUUID.uuid
atomicState.notPaired = false
return true
}
private createLocaleStrings() {
state.domains = [
test : "http://api.encoredtech.com",
prod : "https://api.encoredtech.com:8082/"
]
state.languageString =
[
energyPlan : 30000,
en : [
desc1 : "Tab below to sign in or sign up to Encored EnerTalk smart energy service and authorize SmartThings access.",
desc2 : "Click to proceed authorization.",
main : "EnerTalk",
defaultValues : [
default1 : "kWh",
default2 : "1st day of the month",
default3 : "Low voltage"
],
meteringDays : [
"1st day of the month",
"2nd day of the month",
"3rd day of the month",
"4th day of the month",
"5th day of the month",
"6th day of the month",
"7th day of the month",
"8th day of the month",
"9th day of the month",
"10th day of the month",
"11th day of the month",
"12th day of the month",
"13th day of the month",
"14th day of the month",
"15th day of the month",
"16th day of the month",
"17th day of the month",
"18th day of the month",
"19th day of the month",
"20st day of the month",
"21st day of the month",
"22nd day of the month",
"23rd day of the month",
"24th day of the month",
"25th day of the month",
"26th day of the month",
"Rest of the month"
],
displayUnits : ["WON(₩)", "kWh"],
contractTypes : ["Low voltage", "High voltage"],
title1 : "Send push notification",
title2 : "Energy Plan",
subTitle1 : "Setup your energy plan by won",
title3 : "Display Unit",
title4 : "Metering Date",
title5 : "Contract Type",
title6 : "User & Notifications",
message1 : """ <p>Your Encored Account is now connected to SmartThings!</p> <p>Click 'Done' to finish setup.</p> """,
message2 : """ <p>The connection could not be established!</p> <p>Click 'Done' to return to the menu.</p> """,
message3 : [
header : "Device is not installed",
body1 : "You need to install EnerTalk device at first,",
body2 : "and proceed setup and register device.",
button1 : "Setup device",
button2 : "Not Installed"
],
message4 : [
header : "Device is not connected.",
body1 : "Please check the Wi-Fi network connection",
body2 : "and EnerTalk device status.",
body3 : "Select ‘Setup Device’ to reset the device."
]
],
ko :[
desc1 : "스마트 에너지 서비스를 이용하시려면 EnerTalk 서비스 가입과 SmartThings 접근 권한이 필요합니다.",
desc2 : "아래 버튼을 누르면 인증을 시작합니다",
main : "EnerTalk 인증",
defaultValues : [
default1 : "kWh",
default2 : "매월 1일",
default3 : "주택용 저압"
],
meteringDays : [
"매월 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일",
"말일"
],
displayUnits : ["원(₩)", "kWh"],
contractTypes : ["주택용 저압", "주택용 고압"],
title1 : "알람 설정",
title2 : "사용 계획 (원)",
subTitle1 : "월간 계획을 금액으로 입력하세요",
title3 : "표시 단위",
title4 : "정기검침일",
title5 : "계약종별",
title6 : "사용자 & 알람 설정",
message1 : """ <p>EnerTalk 계정이 SmartThings와 연결 되었습니다!</p> <p>Done을 눌러 계속 진행해 주세요.</p> """,
message2 : """ <p>계정 연결이 실패했습니다.</p> <p>Done 버튼을 눌러 다시 시도해주세요.</p> """,
message3 : [
header : "기기 설치가 필요합니다.",
body1 : "가정 내 분전반에 EnerTalk 기기를 먼저 설치하고,",
body2 : "아래 버튼을 눌러 기기등록 및 연결을 진행하세요.",
button1 : "기기 설정",
button2 : "설치필요"
],
message4 : [
header : "Device is not connected.",
body1 : "Please check the Wi-Fi network connection",
body2 : "and EnerTalk device status.",
body3 : "Select ‘Setup Device’ to reset the device."
]
]
]
}
/* This method makes a redirect url with a given endpoint */
private buildRedirectUrl(mappingPath) {
log.debug "Start : Starting to making a redirect URL with endpoint : /${mappingPath}"
def url = "https://graph.api.smartthings.com/api/token/${state.accessToken}/smartapps/installations/${app.id}/${mappingPath}"
log.debug "Done : Finished to make a URL : ${url}"
url
}
String toQueryString(Map m) {
return m.collect { k, v -> "${k}=${URLEncoder.encode(v.toString())}" }.sort().join("&")
}
/* make a success message. */
private success() {
def lang = clientLocale?.language
if ("${lang}" == "ko") {
log.debug "I was here at first."
atomicState.language = "ko"
} else {
atomicState.language = "en"
}
log.debug atomicState.language
def message = atomicState.languageString."${atomicState.language}".message1
connectionStatus(message)
}
/* make a failure message. */
private fail() {
def lang = clientLocale?.language
if ("${lang}" == "ko") {
log.debug "I was here at first."
atomicState.language = "ko"
} else {
atomicState.language = "en"
}
def message = atomicState.languageString."${atomicState.language}".message2
connectionStatus(message)
}
private connectionStatus(message) {
def html = """
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width height=device-height">
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<title>SmartThings Connection</title>
<style type="text/css">
@font-face {
font-family: 'Swiss 721 W01 Thin';
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.eot');
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.eot?#iefix') format('embedded-opentype'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.woff') format('woff'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.ttf') format('truetype'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.svg#swis721_th_btthin') format('svg');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Swiss 721 W01 Light';
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.eot');
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.eot?#iefix') format('embedded-opentype'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.woff') format('woff'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.ttf') format('truetype'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.svg#swis721_lt_btlight') format('svg');
font-weight: normal;
font-style: normal;
}
body {
margin: 0;
width : 100%;
}
.container {
width: 100%;
/*background: #eee;*/
text-align: center;
}
img {
vertical-align: middle;
margin-top:20.3125vw;
}
.encored{
width: 25vw;
height: 25vw;
margin-right : 8.75vw;
}
.chain {
width:6.25vw;
height: 6.25vw;
}
.smartt {
width: 25vw;
height: 25vw;
margin-left: 8.75vw
}
p {
font-size: 21px;
font-weight: 300;
font-family: Roboto;
text-align: center;
color: #4c4c4e;
margin-bottom: 0;
}
/*
p:last-child {
margin-top: 0px;
}
*/
span {
font-family: 'Swiss 721 W01 Light';
}
</style>
</head>
<body>
<div class="container">
<img class="encored" src="https://s3-ap-northeast-1.amazonaws.com/smartthings-images/appicon_enertalk.png" alt="Encored icon" />
<img class="chain" src="https://s3-ap-northeast-1.amazonaws.com/smartthings-images/icon_link.svg" alt="connected device icon" />
<img class="smartt" src="https://s3.amazonaws.com/smartapp-icons/Partner/support/st-logo%402x.png" alt="SmartThings logo" />
<p>${message}</p>
</div>
</body>
</html>
"""
render contentType: 'text/html', data: html
}
private refreshAuthToken() {
/*Refreshing Encored Access Token*/
log.debug "Refreshing Encored Access Token"
if(!atomicState.encoredRefreshToken) {
log.error "Encored Refresh Token does not exist!"
} else {
def authorization = "Basic " + "${appSettings.clientId}:${appSettings.clientSecret}".bytes.encodeBase64()
def refreshParam = makePostParams("https://enertalk-auth.encoredtech.com/token",
[Authorization: authorization],
[grant_type: 'refresh_token', refresh_token: "${atomicState.encoredRefreshToken}"])
def newAccessToken = getHttpPostJson(refreshParam)
if (newAccessToken) {
atomicState.encoredAccessToken = newAccessToken.access_token
log.debug "Successfully got new Encored Access Token.\n"
} else {
log.error "Was unable to renew Encored Access Token.\n"
}
}
}
private getHttpPutJson(param) {
log.debug "Put URI : ${param.uri}"
try {
httpPut(param) { resp ->
log.debug "HTTP Put Success"
}
} catch(groovyx.net.http.HttpResponseException e) {
log.warn "HTTP Put Error : ${e}"
}
}
private getHttpPostJson(param) {
log.debug "Post URI : ${param.uri}"
def jsonMap = null
try {
httpPost(param) { resp ->
jsonMap = resp.data
log.debug resp.data
}
} catch(groovyx.net.http.HttpResponseException e) {
log.warn "HTTP Post Error : ${e}"
}
return jsonMap
}
private getHttpGetJson(param, testLog) {
log.debug "Get URI : ${param.uri}"
def jsonMap = null
try {
httpGet(param) { resp ->
jsonMap = resp.data
}
} catch(groovyx.net.http.HttpResponseException e) {
log.warn "HTTP Get Error : ${e}"
}
return jsonMap
}
private makePostParams(uri, header, body=[]) {
return [
uri : uri,
headers : header,
body : body
]
}
private makeGetParams(uri, headers, path="") {
return [
uri : uri,
path : path,
headers : headers
]
}
def getInitialData() {
def lang = clientLocale?.language
if ("${lang}" == "ko") {
lang = "ko"
} else {
lang = "en"
}
atomicState.solutionModuleSettings.language = lang
atomicState.solutionModuleSettings
}
def consoleLog() {
log.debug "console log: ${request.JSON.str}"
}
def getHtml() {
/* initializing variables */
def deviceStatusData = "", standbyData = "", meData = "", meteringData = "", rankingData = "", lastMonth = "", deviceId = ""
def standby = "", plan = "", start = "", end = "", meteringDay = "", meteringUsage = "", percent = "", tier = "", meteringPeriodBill = ""
def maxLimitUsageBill, maxLimitUsage = 0
def deviceStatus = false
def displayUnit = "watt"
def meteringPeriodBillShow = "", meteringPeriodBillFalse = "collecting data"
def standbyShow = "", standbyFalse = "collecting data"
def rankingShow = "collecting data"
def tierShow = "collecting data"
def lastMonthShow = "", lastMonthFalse = "no records"
def planShow = "", planFalse = "set up plan"
def thisMonthUnitOne ="", thisMonthUnitTwo = "", planUnitOne = "", planUnitTwo = "", lastMonthUnit = "", standbyUnit = ""
def thisMonthTitle = "This Month", tierTitle = "Billing Tier", planTitle = "Energy Goal",
lastMonthTitle = "Last Month", rankingTitle = "Ranking", standbyTitle = "Always on", energyMonitorDeviceTitle = "EnerTalk Device" , realtimeTitle = "Realtime"
def onOff = "OFF", rankImage = "", tierImage = ""
def htmlBody = ""
/* Get the language setting on device. */
def lang = clientLocale?.language
if ("${lang}" == "ko") {
atomicState.language = "ko"
} else {
atomicState.language = "en"
}
if (atomicState.language == "ko") {
rankingShow = "데이터 수집 중"
meteringPeriodBillFalse = "데이터 수집 중"
lastMonthFalse = "정보가 없습니다"
standbyFalse = "데이터 수집 중"
planFalse = "계획을 입력하세요"
thisMonthTitle = "이번 달"
tierTitle = "누진단계"
planTitle = "사용 계획"
lastMonthTitle = "지난달"
rankingTitle = "랭킹"
standbyTitle = "대기전력"
energyMonitorDeviceTitle = "스마트미터 상태"
realtimeTitle = "실시간"
}
/* check Encored Access Token */
def EATValidation = checkEncoreAccessTokenValidation()
log.debug EATValidation
/* check if uuid already exist or not.*/
if (EATValidation && atomicState.notPaired) {
getUUID()
}
/* If token has been verified or refreshed and if uuid exist, call other apis */
log.debug atomicState.notPaired
if (!atomicState.notPaired) {
if(EATValidation) {
/* make a parameter to get device status */
def deviceStatusParam = makeGetParams( "${state.domains."${atomicState.env_mode}"}/1.2/devices/${atomicState.uuid}/status",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
/* get device status. */
deviceStatusData = getHttpGetJson(deviceStatusParam, 'CheckDeviceStatus')
/* make a parameter to get standby value.*/
def standbyParam = makeGetParams( "${state.domains."${atomicState.env_mode}"}/1.2/devices/${atomicState.uuid}/standbyPower",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
/* get standby value */
standbyData = getHttpGetJson(standbyParam, 'CheckStandbyPower')
/* make a parameter to get user's info. */
def meParam = makeGetParams( "${state.domains."${atomicState.env_mode}"}/1.2/me",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
/* Get user's info */
meData = getHttpGetJson(meParam, 'CheckMe')
/* make a parameter to get energy used since metering date */
def meteringParam = makeGetParams( "${state.domains."${atomicState.env_mode}"}/1.2/devices/${atomicState.uuid}/meteringUsage",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
/* Get the value of energy used since metering date. */
meteringData = getHttpGetJson(meteringParam, 'CheckMeteringUsage')
/* make a parameter to get the energy usage ranking of a user. */
def rankingParam = makeGetParams( "${state.domains."${atomicState.env_mode}"}/1.2/ranking/usages/${atomicState.uuid}?state=current&period=monthly",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
/* Get user's energy usage rank */
rankingData = getHttpGetJson(rankingParam, 'CheckingRanking')
/* Parse the values from the returned value of api calls. Then use these values to inform user how much they have used or will use. */
/* parse device status. */
if (deviceStatusData) {
if (deviceStatusData.status == "NORMAL") {
deviceStatus = true
}
}
log.debug "deiceStatusData : ${deviceStatus} || ${deviceStatusData}"
/* Parse standby power. */
if (standbyData) {
if (standbyData.standbyPower) {
standby = (standbyData.standbyPower / 1000)
}
}
/* Parse max limit usage and it's bill from user's info. */
if (meData) {
if (meData.maxLimitUsageBill) {
maxLimitUsageBill = meData.maxLimitUsageBill
maxLimitUsage = meData.maxLimitUsage
}
}
/* Parse the values which have been used since metering date.
* The list is :
* meteringPeriodBill : A bill for energy usage.
* plan : The left amount of bill until it reaches limit.
* start : metering date in millisecond e.g. if the metering started on june and 1st, 2015,06,01
* end : Today's date in millisecond
* meteringDay : The day of the metering date. e.g. if the metering date is June 1st, then it will return 1.
* meteringUSage : The amount of energy that user has used.
* tier : the level of energy use, tier exits from 1 to 6.
*/
if (meteringData) {
if (meteringData.meteringPeriodBill) {
meteringPeriodBill = meteringData.meteringPeriodBill
plan = maxLimitUsageBill - meteringData.meteringPeriodBill
start = meteringData.meteringStart
end = meteringData.meteringEnd
meteringDay = meteringData.meteringDay
meteringUsage = meteringData.meteringPeriodUsage
tier = ((int) (meteringData.meteringPeriodUsage / 100000000) + 1)
if(tier > 6) {
tier = 6
}
}
}
/* Get ranking data of a user and the percent */
if (rankingData) {
if (rankingData.user.ranking) {
percent = ((int)((rankingData.user.ranking / rankingData.user.population) * 10))
if (percent > 10) {
percent = 10
}
}
}
/* if the start value exist, get last month energy usage. */
if (start) {
def lastMonthParam = makeGetParams( "${state.domains."${atomicState.env_mode}"}/1.2/devices/${atomicState.uuid}/meteringUsages?period=monthly&start=${start}&end=${end}",
[Authorization: "Bearer ${atomicState.encoredAccessToken}", ContentType: "application/json"])
lastMonth = getHttpGetJson(lastMonthParam, 'ChecklastMonth')
}
/* I decided to set values to device type handler, on loading solution module.
So, users may need to go back to solution module to update their device type handler. */
def d = getChildDevice(atomicState.dni)
def kWhMonth = Math.round(meteringUsage / 10000) / 100 /* milliwatt to kilowatt*/
def planUsed = 0
if ( maxLimitUsage > 0 ) {
planUsed = Math.round((meteringUsage / maxLimitUsage) * 100) /* get the pecent of used amount against max usage */
} else {
planUsed = Math.round((meteringUsage/ 1000000) * 100) /* if max was not decided let the used value be percent. e.g. 1kWh = 100% */
}
/* get realtime usage of user's device.*/
def realTimeParam = makeGetParams("${state.domains."${atomicState.env_mode}"}/1.2/devices/${atomicState.uuid}/realtimeUsage",
[Authorization: "Bearer ${atomicState.encoredAccessToken}"])
def realTimeInfo = getHttpGetJson(realTimeParam, 'CheckRealtimeinfo')
if (!realTimeInfo) {
realTimeInfo = 0
} else {
realTimeInfo = Math.round(realTimeInfo.activePower / 1000 )
}
/* inserting values to device type handler */
d?.sendEvent(name: "view", value : "${kWhMonth}")
if (deviceStatus) {
d?.sendEvent(name: "month", value : "${thisMonthTitle} \n ${kWhMonth} \n kWh")
} else {
d?.sendEvent(name: "month", value : "\n ${state.languageString."${atomicState.language}".message4.header} \n\n " +
"${state.languageString."${atomicState.language}".message4.body1} \n " +
"${state.languageString."${atomicState.language}".message4.body2} \n " +