1/** 2 * API utilities service 3 * 4 * @module app/common/services/api-utils 5 * @exports APIUtils 6 * @name APIUtils 7 * @version 0.0.1 8 */ 9 10window.angular && (function (angular) { 11 'use strict'; 12 angular 13 .module('app.common.services') 14 .factory('APIUtils', ['$http', 'Constants', function($http, Constants){ 15 var SERVICE = { 16 LOGIN_CREDENTIALS: Constants.LOGIN_CREDENTIALS, 17 API_CREDENTIALS: Constants.API_CREDENTIALS, 18 API_RESPONSE: Constants.API_RESPONSE, 19 CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE, 20 HOST_STATE_TEXT: Constants.HOST_STATE, 21 HOST_STATE: Constants.HOST_STATE, 22 getChassisState: function(callback){ 23 $http({ 24 method: 'GET', 25 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/chassis0", 26 headers: { 27 'Accept': 'application/json', 28 'Content-Type': 'application/json' 29 }, 30 withCredentials: true 31 }).success(function(response){ 32 var json = JSON.stringify(response); 33 var content = JSON.parse(json); 34 callback(content.data.CurrentPowerState); 35 }).error(function(error){ 36 console.log(error); 37 }); 38 }, 39 getHostState: function(callback){ 40 $http({ 41 method: 'GET', 42 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", 43 headers: { 44 'Accept': 'application/json', 45 'Content-Type': 'application/json' 46 }, 47 withCredentials: true 48 }).success(function(response){ 49 var json = JSON.stringify(response); 50 var content = JSON.parse(json); 51 callback(content.data.CurrentHostState); 52 }).error(function(error){ 53 console.log(error); 54 }); 55 }, 56 login: function(username, password, callback){ 57 $http({ 58 method: 'POST', 59 url: SERVICE.API_CREDENTIALS.host + "/login", 60 headers: { 61 'Accept': 'application/json', 62 'Content-Type': 'application/json' 63 }, 64 withCredentials: true, 65 data: JSON.stringify({"data": [username, password]}) 66 }).success(function(response){ 67 if(callback){ 68 callback(response); 69 } 70 }).error(function(error){ 71 if(callback){ 72 callback(null, true); 73 } 74 console.log(error); 75 }); 76 }, 77 logout: function(callback){ 78 $http({ 79 method: 'POST', 80 url: SERVICE.API_CREDENTIALS.host + "/logout", 81 headers: { 82 'Accept': 'application/json', 83 'Content-Type': 'application/json' 84 }, 85 withCredentials: true, 86 data: JSON.stringify({"data": []}) 87 }).success(function(response){ 88 if(callback){ 89 callback(response); 90 } 91 }).error(function(error){ 92 if(callback){ 93 callback(null, error); 94 } 95 console.log(error); 96 }); 97 }, 98 chassisPowerOn: function(callback){ 99 $http({ 100 method: 'POST', 101 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", 102 headers: { 103 'Accept': 'application/json', 104 'Content-Type': 'application/json' 105 }, 106 withCredentials: true, 107 data: JSON.stringify({"data": []}) 108 }).success(function(response){ 109 var json = JSON.stringify(response); 110 var content = JSON.parse(json); 111 if(callback){ 112 return callback(content.data.CurrentPowerState); 113 } 114 }).error(function(error){ 115 if(callback){ 116 callback(error); 117 }else{ 118 console.log(error); 119 } 120 }); 121 }, 122 chassisPowerOff: function(callback){ 123 $http({ 124 method: 'POST', 125 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", 126 headers: { 127 'Accept': 'application/json', 128 'Content-Type': 'application/json' 129 }, 130 withCredentials: true, 131 data: JSON.stringify({"data": []}) 132 }).success(function(response){ 133 var json = JSON.stringify(response); 134 var content = JSON.parse(json); 135 if(callback){ 136 return callback(content.data.CurrentPowerState); 137 } 138 }).error(function(error){ 139 if(callback){ 140 callback(error); 141 }else{ 142 console.log(error); 143 } 144 }); 145 }, 146 hostPowerOn: function(callback){ 147 /** 148 curl -c cjar -b cjar -k -H "Content-Type: application/json" -d 149 "{\"data\": \"xyz.openbmc_project.State.Host.Transition.Off\"}" 150 -X PUT 151 https://9.3.164.147/xyz/openbmc_project/state/host0/attr/RequestedHostTransition 152 **/ 153 $http({ 154 method: 'PUT', 155 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition", 156 headers: { 157 'Accept': 'application/json', 158 'Content-Type': 'application/json' 159 }, 160 withCredentials: true, 161 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"}) 162 }).success(function(response){ 163 var json = JSON.stringify(response); 164 var content = JSON.parse(json); 165 if(callback){ 166 return callback(content.status); 167 } 168 }).error(function(error){ 169 if(callback){ 170 callback(error); 171 }else{ 172 console.log(error); 173 } 174 }); 175 }, 176 hostPowerOff: function(callback){ 177 $http({ 178 method: 'PUT', 179 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition", 180 headers: { 181 'Accept': 'application/json', 182 'Content-Type': 'application/json' 183 }, 184 withCredentials: true, 185 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"}) 186 }).success(function(response){ 187 var json = JSON.stringify(response); 188 var content = JSON.parse(json); 189 if(callback){ 190 return callback(content.status); 191 } 192 }).error(function(error){ 193 if(callback){ 194 callback(error); 195 }else{ 196 console.log(error); 197 } 198 }); 199 }, 200 hostReboot: function(callback){ 201 $http({ 202 method: 'POST', 203 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", 204 headers: { 205 'Accept': 'application/json', 206 'Content-Type': 'application/json' 207 }, 208 withCredentials: true, 209 data: JSON.stringify({"data": []}), 210 }).success(function(response){ 211 var json = JSON.stringify(response); 212 var content = JSON.parse(json); 213 if(callback){ 214 return callback(content); 215 } 216 }).error(function(error){ 217 if(callback){ 218 callback(error); 219 }else{ 220 console.log(error); 221 } 222 }); 223 }, 224 hostShutdown: function(callback){ 225 $http({ 226 method: 'POST', 227 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0", 228 headers: { 229 'Accept': 'application/json', 230 'Content-Type': 'application/json' 231 }, 232 withCredentials: true, 233 data: JSON.stringify({"data": []}) 234 }).success(function(response){ 235 var json = JSON.stringify(response); 236 var content = JSON.parse(json); 237 if(callback){ 238 return callback(content); 239 } 240 }).error(function(error){ 241 if(callback){ 242 callback(error); 243 }else{ 244 console.log(error); 245 } 246 }); 247 } 248 }; 249 return SERVICE; 250 }]); 251 252 })(window.angular); 253