initial upload
[apps/native/smart-surveillance-camera.git] / smartthings-plugin / plugin / js / capability_servoMotor.js
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 const SERVO_RANGE = {
18         VERTICAL: {
19                 MIN: 15,
20                 MAX: 65
21         },
22         HORIZONTAL: {
23                 MIN: 5,
24                 MAX: 65
25         }
26 };
27 const SERVO_STEP = {
28         VERTICAL: (SERVO_RANGE.VERTICAL.MAX - SERVO_RANGE.VERTICAL.MIN) / 10,
29         HORIZONTAL: (SERVO_RANGE.HORIZONTAL.MAX - SERVO_RANGE.HORIZONTAL.MIN) / 10,
30 };
31 const CONTROL_TYPE_VERTICAL = "vertical";
32 const CONTROL_TYPE_HORIZONTAL = "horizontal";
33
34 var capabilityServoMotor = {
35         'href' : {
36                 v: "/capability/audioVolume/main/0",
37                 h: "/capability/switchLevel/main/0"
38         },
39         'valueV' : 50,
40         'valueH' : 50,
41
42         'update' : function() {
43                 for (var item in this.href) {
44                         ocfDevice.getRemoteRepresentation(this.href[item], this.onRepresentCallback);
45                 }
46         },
47
48         'onRepresentCallback' : function(result, deviceHandle, uri, rcsJsonString) {
49                 scplugin.log.debug(className, arguments.callee.name, result);
50                 scplugin.log.debug(className, arguments.callee.name, uri);
51
52                 if (result == "OCF_OK" || result == "OCF_RESOURCE_CHANGED" || result == "OCF_RES_ALREADY_SUBSCRIBED") {
53                         if (uri == capabilityServoMotor.href.v) {
54                                 if(rcsJsonString["volume"] === undefined) return;
55                                 capabilityServoMotor.valueV = rcsJsonString["volume"];
56                         } else if (uri == capabilityServoMotor.href.h) {
57                                 if(rcsJsonString["dimmingSetting"] === undefined) return;
58                                 capabilityServoMotor.valueH = rcsJsonString["dimmingSetting"];
59                         }
60                 }
61         },
62
63         'move' : function(type, value) {
64                 var setRcsJson = {};
65                 var href;
66                 scplugin.log.debug(className, arguments.callee.name, "Servo Motor type: " + type, "value: " + value);
67
68                 if (type == CONTROL_TYPE_VERTICAL) {
69                         setRcsJson["volume"] = value;
70                         href = this.href.v;
71                 } else if (type == CONTROL_TYPE_HORIZONTAL) {
72                         setRcsJson["dimmingSetting"] = value;
73                         href = this.href.h;
74                 } else {
75                         scplugin.log.debug(className, arguments.callee.name, "Servo Motor invalid type!");
76                 }
77
78                 ocfDevice.setRemoteRepresentation(href, setRcsJson, this.onRepresentCallback);
79         },
80
81         'moveDown' : function() {
82                 var nextVal = this.valueV + SERVO_STEP.VERTICAL;
83                 if (nextVal >= SERVO_RANGE.VERTICAL.MAX)
84                         nextVal = SERVO_RANGE.VERTICAL.MAX;
85
86                 this.move(CONTROL_TYPE_VERTICAL, nextVal);
87         },
88
89         'moveUp' : function() {
90                 var nextVal = this.valueV - SERVO_STEP.VERTICAL;
91                 if (nextVal <= SERVO_RANGE.VERTICAL.MIN)
92                         nextVal = SERVO_RANGE.VERTICAL.MIN;
93
94                 this.move(CONTROL_TYPE_VERTICAL, nextVal);
95         },
96         'moveLeft' : function() {
97                 var nextVal = this.valueH + SERVO_STEP.HORIZONTAL;
98                 if (nextVal >= SERVO_RANGE.HORIZONTAL.MAX)
99                         nextVal = SERVO_RANGE.HORIZONTAL.MAX;
100
101                 this.move(CONTROL_TYPE_HORIZONTAL, nextVal);
102         },
103         'moveRight' : function() {
104                 var nextVal = this.valueH - SERVO_STEP.HORIZONTAL;
105                 if (nextVal <= SERVO_RANGE.HORIZONTAL.MIN)
106                         nextVal = SERVO_RANGE.HORIZONTAL.MIN;
107
108                 this.move(CONTROL_TYPE_HORIZONTAL, nextVal);
109         }
110 }