parameters: (self)
docstring: @api {post} /rest/device Create a device
          @apiName postDevice
          @apiGroup Device
  
          @apiParamTitle (data) Post parameters
          @apiParam (data) {Json} params The populated device params json string
  
          @apiSuccess {json} result The json representation of the created device
  
          @apiSuccessExample Success-Response:
              HTTTP/1.1 201 Created
              {
                  "xpl_stats": {
                      "get_temp": {
                          "json_id": "get_temp",
                          "schema": "sensor.basic",
                          "id": 4,
                          "parameters": {
                              "dynamic": [
                                  {
                                      "ignore_values": "",
                                      "sensor_name": "temp",
                                      "key": "current"
                                  }
                              ],
                              "static": [
                                  {
                                      "type": "integer",
                                      "value": "2",
                                      "key": "device"
                                  },
                                  {
                                      "type": null,
                                      "value": "temp",
                                      "key": "type"
                                  },
                                  {
                                      "type": null,
                                      "value": "c",
                                      "key": "units"
                                  }
                              ]
                          },
                          "name": "get_temp"
                      },
                      ...
                  },
                  "commands": {
                      ...
                  },
                  "description": "Test Temp",
                  "reference": "VMB1TS",
                  "xpl_commands": {
                      ...
                  },
                  "client_id": "plugin-velbus.igor",
                  "device_type_id": "velbus.temp",
                  "sensors": {
                      "temp": {
                          "value_min": 21.875,
                          "data_type": "DT_Temp",
                          "incremental": false,
                          "id": 4,
                          "reference": "temp",
                          "conversion": "",
                          "name": "temp_sensor",
                          "last_received": 1410857216,
                          "timeout": 0,
                          "formula": null,
                          "last_value": "29.1875",
                          "value_max": 37.4375
                      }
                  },
                  "parameters": {
                      ...
                  },
                  "id": 3,
                  "name": "Temp elentrik"
              }
  
          @apiErrorExample Error
              HTTTP/1.1 500
              {
                  'error': '...'
              }
docstring_summary: @api {post} /rest/device Create a device
          @apiName postDevice
          @apiGroup Device
docstring_tokens: @api { post } / rest / device Create a device @apiName postDevice @apiGroup Device
function: def post(self):
          """
          @api {post} /rest/device Create a device
          @apiName postDevice
          @apiGroup Device
  
          @apiParamTitle (data) Post parameters
          @apiParam (data) {Json} params The populated device params json string
  
          @apiSuccess {json} result The json representation of the created device
  
          @apiSuccessExample Success-Response:
              HTTTP/1.1 201 Created
              {
                  "xpl_stats": {
                      "get_temp": {
                          "json_id": "get_temp",
                          "schema": "sensor.basic",
                          "id": 4,
                          "parameters": {
                              "dynamic": [
                                  {
                                      "ignore_values": "",
                                      "sensor_name": "temp",
                                      "key": "current"
                                  }
                              ],
                              "static": [
                                  {
                                      "type": "integer",
                                      "value": "2",
                                      "key": "device"
                                  },
                                  {
                                      "type": null,
                                      "value": "temp",
                                      "key": "type"
                                  },
                                  {
                                      "type": null,
                                      "value": "c",
                                      "key": "units"
                                  }
                              ]
                          },
                          "name": "get_temp"
                      },
                      ...
                  },
                  "commands": {
                      ...
                  },
                  "description": "Test Temp",
                  "reference": "VMB1TS",
                  "xpl_commands": {
                      ...
                  },
                  "client_id": "plugin-velbus.igor",
                  "device_type_id": "velbus.temp",
                  "sensors": {
                      "temp": {
                          "value_min": 21.875,
                          "data_type": "DT_Temp",
                          "incremental": false,
                          "id": 4,
                          "reference": "temp",
                          "conversion": "",
                          "name": "temp_sensor",
                          "last_received": 1410857216,
                          "timeout": 0,
                          "formula": null,
                          "last_value": "29.1875",
                          "value_max": 37.4375
                      }
                  },
                  "parameters": {
                      ...
                  },
                  "id": 3,
                  "name": "Temp elentrik"
              }
  
          @apiErrorExample Error
              HTTTP/1.1 500
              {
                  'error': '...'
              }
          """
          try:
              app.logger.debug(u"Form params : {0}".format(request.form))
              params = json.loads(request.form.get('params'))
              status = True
              print params
              cli = MQSyncReq(app.zmq_context)
              msg = MQMessage()
              msg.set_action('device_types.get')
              msg.add_data('device_type', params['device_type'])
              res = cli.request('manager', msg.get(), timeout=10)
              del cli
              if res is None:
                  status = False
                  reason = "Manager is not replying to the mq request"
              pjson = res.get_data()
              if pjson is None:
                  status = False
                  reason = "No data for {0} found by manager".format(params['device_type'])
              pjson = pjson[params['device_type']]
              if pjson is None:
                  status = False
                  reason = "The json for {0} found by manager is empty".format(params['device_type'])
      
              with app.db.session_scope():
                  if status:
                      # call the add device function
                      res = app.db.add_full_device(params, pjson)
                      if not res:
                          status = False
                          reason = "An error occured while adding the device in database. Please check the file admin.log for more informations"
                      else:
                          status = True
                          reason = False
                          result = res
                  if status:
                      return 201, result
                  else:
                      return 500, {'error' : reason}
          except:
              msg = u"Error while creating the device. Error is : {0}".format(traceback.format_exc())
              app.logger.error(msg)
              return 500, {'error': msg}
function_tokens: def post ( self ) : try : app . logger . debug ( u"Form params : {0}" . format ( request . form ) ) params = json . loads ( request . form . get ( 'params' ) ) status = True print params cli = MQSyncReq ( app . zmq_context ) msg = MQMessage ( ) msg . set_action ( 'device_types.get' ) msg . add_data ( 'device_type' , params [ 'device_type' ] ) res = cli . request ( 'manager' , msg . get ( ) , timeout = 10 ) del cli if res is None : status = False reason = "Manager is not replying to the mq request" pjson = res . get_data ( ) if pjson is None : status = False reason = "No data for {0} found by manager" . format ( params [ 'device_type' ] ) pjson = pjson [ params [ 'device_type' ] ] if pjson is None : status = False reason = "The json for {0} found by manager is empty" . format ( params [ 'device_type' ] ) with app . db . session_scope ( ) : if status : # call the add device function res = app . db . add_full_device ( params , pjson ) if not res : status = False reason = "An error occured while adding the device in database. Please check the file admin.log for more informations" else : status = True reason = False result = res if status : return 201 , result else : return 500 , { 'error' : reason } except : msg = u"Error while creating the device. Error is : {0}" . format ( traceback . format_exc ( ) ) app . logger . error ( msg ) return 500 , { 'error' : msg }