initial upload
[apps/native/smart-surveillance-camera.git] / smartthings-plugin / plugin / js / index.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 var ocfDevice;
18 var capabilities = [capabilityModeSwitch, capabilityMotionSensor, capabilityServoMotor];
19 var className = "iot2018";
20
21 window.onload = function () {
22         console.log("version : 0.0.1");
23         registerTouchEvents();
24         init();
25 };
26
27 function init() {
28         console.log("-----------init-----------");
29         scplugin.manager.getOCFDevices(getOCFDeviceCB);
30 }
31
32 function getOCFDeviceCB(devices) {
33         console.log("getOCFDeviceCB : " + devices.length);
34
35         for (var i in devices) {
36                 console.log("deviceHandle: " + devices[i].deviceHandle);
37                 console.log("deviceName: " + devices[i].deviceName);
38                 console.log("deviceType: " + devices[i].deviceType);
39                 console.log("metadata: " + devices[i].metadata);
40         }
41
42         setMainDevice(devices[0]);
43         ocfDevice.subscribe(onRepresentCallback);
44
45         for (var i = 0; i < capabilities.length; i++) {
46                 capabilities[i].update();
47         }
48 }
49
50 function onRepresentCallback(result, deviceHandle, uri, rcsJsonString) {
51         for (var i = 0; i < capabilities.length; i++) {
52                 var hrefObj = capabilities[i].href;
53
54                 for (var item in hrefObj) {
55                         if (hrefObj[item] == uri)
56                                 capabilities[i].onRepresentCallback(result, deviceHandle, uri, rcsJsonString);
57                 }
58         }
59 }
60
61 function registerTouchEvents() {
62         var elements = document.getElementsByClassName("icon-move");
63         // up-left-right-down
64
65         // Move Up
66         elements[0].addEventListener("touchstart", function() {
67                 if (!capabilityModeSwitch.isControllable()){
68                         return;
69                 }
70                 document.getElementById("camera_controller").className = "shadow-up";
71         }, false);
72
73         // Move Left
74         elements[1].addEventListener("touchstart", function() {
75                 if (!capabilityModeSwitch.isControllable()){
76                         return;
77                 }
78                 document.getElementById("camera_controller").className = "shadow-left";
79         }, false);
80
81         // Move Right
82         elements[2].addEventListener("touchstart", function() {
83                 if (!capabilityModeSwitch.isControllable()){
84                         return;
85                 }
86                 document.getElementById("camera_controller").className = "shadow-right";
87         }, false);
88
89         // Move Down
90         elements[3].addEventListener("touchstart", function() {
91                 if (!capabilityModeSwitch.isControllable()){
92                         return;
93                 }
94                 document.getElementById("camera_controller").className = "shadow-down";
95         }, false);
96
97         for (var i = 0; i < elements.length; i++){
98                 elements[i].addEventListener("touchend", function() {
99                         document.getElementById("camera_controller").className = "";
100                 }, false);
101         }
102 }
103
104 function setMainDevice(device) {
105         scplugin.log.debug(className, arguments.callee.name, "set ocf device : " + device.deviceName);
106         ocfDevice = device;
107 }
108
109 function backAction() {
110         scplugin.manager.close();
111 }
112
113 function onModeBtnClicked() {
114         capabilityModeSwitch.modeToggle();
115 }
116
117 function onMoveUpBtnClicked() {
118         if (!capabilityModeSwitch.isControllable()){
119                 return;
120         }
121         capabilityServoMotor.moveUp();
122 }
123
124 function onMoveDownBtnClicked() {
125         if (!capabilityModeSwitch.isControllable()){
126                 return;
127         }
128         capabilityServoMotor.moveDown();
129 }
130
131 function onMoveLeftBtnClicked() {
132         if (!capabilityModeSwitch.isControllable()){
133                 return;
134         }
135         capabilityServoMotor.moveLeft();
136 }
137
138 function onMoveRightBtnClicked() {
139         if (!capabilityModeSwitch.isControllable()){
140                 return;
141         }
142         capabilityServoMotor.moveRight();
143 }