def post(self, action):
        #url = 'http://143.248.49.87:88/api/XAI8Yvp26NTLSxP0uGurWuI091Qxj65C2VFjSsr2/lights/2/state'

        # I don't know exact reason but, when I don't call action by 'self', hueAPI can't recognize redis variable in superclass
        '''
        if action == "on":
            on_message_body = {"on": True, "sat": 254, "bri": 254, "hue": 10000}
            res = requests.put(url, data=json.dumps(on_message_body))
            if res.status_code == 200:
                return jsonify(res.text), 200
            else: abort(400, description="abort in post() for on")

        elif action == "off":
            off_message_body = {"on": False}
            res = requests.put(url, data=json.dumps(off_message_body))
            if res.status_code == 200:
                return jsonify(res.text), 200
            else: abort(400, description="abort in post() for off")

        else: abort(400, description="wrong action name")
        '''
        if action == "on":
            return self.on()

        elif action == "off":
            return self.off()

        else:
            #abort(400, description="Invalid action")
            abort_json(HTTPStatus.BAD_REQUEST, "Invalid action.")
 def status(self): # not used -> deprecated
     try:
         res = requests.get(self.hue_urls['status'])
         text_res = res.text
     except:
         abort_json(HTTPStatus.BAD_REQUEST, "abort in status()")
     return make_response(jsonify(text_res), HTTPStatus.OK)
 def get(self):
     #url = 'http://143.248.49.87:88/api/XAI8Yvp26NTLSxP0uGurWuI091Qxj65C2VFjSsr2/lights'
     try: 
         #return self.status()
         return self.status_newV()
     except:
         #abort(400, description="abort in get()")
         abort_json(HTTPStatus.BAD_REQUEST, "abort in get()")
    def off(self):
        off_message_body = {"on": False}
        #res = requests.put('http://143.248.49.87:88/api/XAI8Yvp26NTLSxP0uGurWuI091Qxj65C2VFjSsr2/lights/2/state',data=json.dumps(off_message_body))
        
        res = requests.put(self.hue_urls['action'],data=json.dumps(off_message_body))

        self.redis.set('status', "Off")
        if res.status_code == 200:
            #return make_response(jsonify(res.text), HTTPStatus.OK)
            return make_response(jsonify({"status": self.redis.get('status')}), 200)
        else:
            #abort(400, description="abort in post() for off")
            abort_json(HTTPStatus.BAD_REQUEST, "abort in post() about off func")
Esempio n. 5
0
    def unbind(self):
        # Read user id from HTTP request header
        user_id = request.headers.get('USER-ID')

        # Raise 401 error if the resource is not bound
        if not self.redis.exists('user_id'):
            abort_json(HTTPStatus.UNAUTHORIZED, "Resource not bound.")

        # Unbind user successfully
        else:
            user_id = self.redis.get('user_id')
            self.redis.delete('user_id')

            response = make_response(jsonify({"userId": user_id}),
                                     HTTPStatus.OK)
            return response
Esempio n. 6
0
    def bind(self):
        # Read user id from HTTP request header
        user_id = request.headers.get('USER-ID')

        # Raise 409 Conflict error if the resource is already bound to another user
        if self.redis.exists(
                'user_id') and self.redis.get('user_id') != user_id:
            abort_json(HTTPStatus.CONFLICT, "Resource bound to another user.")

        # Bind user successfully
        else:
            self.redis.set('user_id', user_id)

            response = make_response(
                jsonify({"userId": self.redis.get('user_id')}), HTTPStatus.OK)
            return response
Esempio n. 7
0
    def post(self, action):
        """
        post: receive bind or unbind action from users
        :param action: command on the resource, which is specified on URL
        :return: [flask HTTP response in JSON] action acceptance result
        """
        # Bind action
        if action == "bind":
            return self.bind()

        # Unbind action
        elif action == "unbind":
            return self.unbind()

        # Reject other actions
        else:
            abort_json(HTTPStatus.BAD_REQUEST, "Invalid action.")
    def status_newV(self):
        res = requests.get(self.hue_urls['status'])
        if res.status_code == 200:
            #res_dict = json.loads(res) # error
            content = res.content
            content.decode('utf-8')
            res_dict = json.loads(content)

            hueState = res_dict['2']
            state = hueState['state']
            if state['on'] == True:
                self.redis.set('status', "On")
                return make_response(jsonify({"status": self.redis.get('status')}), 200)
            else:
                self.redis.set('status', "Off")
                return make_response(jsonify({"status": self.redis.get('status')}), 200)
        else:
            abort_json(HTTPStatus.BAD_REQUEST, "abort in status()")
Esempio n. 9
0
 def post(self, action):
     print(action)
     if action == "fake":
         return self.fake()
     else:
         return abort_json(HTTPStatus.BAD_REQUEST, "Invalid action.")