func DeviceCmdUpdate(
	deviceHiveURL, deviceGuid, accessKey string,
	commandId uint32,
	status, result string, // can be empty
) (err error) {
	api := gopencils.Api(deviceHiveURL)

	resource := api.Res("device").Id(deviceGuid).Res("command").Id(fmt.Sprintf("%d", commandId))

	if resource.Headers == nil {
		resource.Headers = http.Header{}
	}
	resource.Headers["Authorization"] = []string{"Bearer " + accessKey}

	requestBody := map[string]interface{}{}

	if status != "" {
		requestBody["status"] = status
	}

	if result != "" {
		requestBody["result"] = result
	}

	resource.Put(requestBody)

	if err == nil {
		err = resource.ProcessedError()
	}
	return
}
func DeviceCmdInsert(deviceHiveURL, deviceGuid, accessKey, command string, parameters interface{}) (dcir DeviceCmdInsertResponse, err error) {
	api := gopencils.Api(deviceHiveURL)

	resource := api.Res("device").Id(deviceGuid).Res("command")
	resource.Response = &dcir
	if resource.Headers == nil {
		resource.Headers = http.Header{}
	}
	resource.Headers["Authorization"] = []string{"Bearer " + accessKey}

	requestBody := map[string]interface{}{
		"command": command,
	}

	if parameters != nil {
		requestBody["parameters"] = parameters
	}

	resource.Post(requestBody)

	// log.Printf("DeviceCmdInsert RESPONSE STATUS: %s", resource.Raw.Status)
	// log.Printf("DeviceCmdInsert RESPONSE: %+v", resource.Response)

	if err == nil {
		err = resource.ProcessedError()
	}
	return
}
Exemple #3
0
func GetApiInfo(deviceHiveURL string) (ai ApiInfo, err error) {
	api := gopencils.Api(deviceHiveURL)
	resp := &ai
	resource, err := api.Res("info", resp).Get()
	if err == nil {
		err = resource.ProcessedError()
	}
	return
}
func DeviceRegisterEasy(deviceHiveURL, deviceGuid, accessKey, deviceName, deviceKey, networkName, networkKey, networkDesc string) (err error) {
	api := gopencils.Api(deviceHiveURL)

	resource := api.Res("device").Id(deviceGuid)
	resource.SetHeader("Authorization", "Bearer "+accessKey)

	// device class
	dc := map[string]interface{}{
		"name":           "go-gateway-class",
		"version":        "0.1",
		"offlineTimeout": 10}

	// network (optional)
	n := map[string]interface{}{
		"name":        networkName,
		"key":         networkKey,
		"description": networkDesc}

	// device
	d := map[string]interface{}{
		// [optional] "key":    deviceKey,
		"name":        deviceName,
		"status":      "Online",
		"deviceClass": dc}

	// omit "network" if all fields are empty
	if len(networkName) != 0 || len(networkKey) != 0 || len(networkDesc) != 0 {
		d["network"] = n
	}

	// omit device key if empty
	if len(deviceKey) != 0 {
		d["key"] = deviceKey
	}

	resp, err2 := resource.Put(d)
	say.Infof("DeviceRegisterEasy Response: %+v \r\n %+v", resp, resp.Raw)

	if err2 == nil {
		err = resource.ProcessedError()
	}
	return
}
func DeviceNotificationInsert(deviceHiveURL, deviceGuid, accessKey, name string, parameters interface{}) (dnir DeviceNotificationInsertResponse, err error) {

	api := gopencils.Api(deviceHiveURL)

	resource := api.Res("device").Id(deviceGuid).Res("notification")
	resource.Response = &dnir
	if resource.Headers == nil {
		resource.Headers = http.Header{}
	}
	resource.Headers["Authorization"] = []string{"Bearer " + accessKey}

	requestBody := map[string]interface{}{"notification": name}
	if parameters != nil {
		requestBody["parameters"] = parameters
	}

	resource.Post(requestBody)
	if err == nil {
		err = resource.ProcessedError()
	}

	return

}