Example #1
0
func samplesToJsonObj(samples []cloudvar.CloudVarSample) map[string]interface{} {
	out := map[string]interface{}{}
	out["samples"] = []interface{}{}
	for _, sample := range samples {
		out["samples"] = append(out["samples"].([]interface{}), map[string]interface{}{
			"t": canotime.EpochMicroseconds(sample.Timestamp),
			"v": sample.Value,
		})
	}
	return out
}
Example #2
0
// Constructs the response body for the /api/info REST endpoint
func GET__api__info(info *RestRequestInfo, sideEffect *RestSideEffects) (map[string]interface{}, RestError) {
	t := time.Now().UTC()
	return map[string]interface{}{
		"config":       info.Config.ToJsonObject(),
		"result":       "ok",
		"clock_us":     canotime.EpochMicroseconds(t),
		"clock_utc":    canotime.RFC3339(t),
		"service-name": "Canopy Cloud Service",
		"version":      info.Config.BuildVersion(),
		"build-date":   info.Config.BuildDate(),
		"build-commit": info.Config.BuildCommit(),
	}, nil
}
Example #3
0
func deviceToJsonObj(device datalayer.Device, timestamp_type string) (map[string]interface{}, error) {
	statusJsonObj := map[string]interface{}{
		"ws_connected": device.WSConnected(),
	}
	lastSeen := device.LastActivityTime()
	if lastSeen == nil {
		statusJsonObj["last_activity_time"] = nil
	} else {
		if timestamp_type == "epoch_us" {
			statusJsonObj["last_activity_time"] = canotime.EpochMicroseconds(*lastSeen)
		} else {
			statusJsonObj["last_activity_time"] = canotime.RFC3339(*lastSeen)
		}
	}

	out := map[string]interface{}{
		"device_id":     device.ID().String(),
		"friendly_name": device.Name(),
		"location_note": device.LocationNote(),
		"status":        statusJsonObj,
		"var_decls":     nil,
		"secret_key":    device.SecretKey(),
		"vars":          map[string]interface{}{},
		"notifs":        []interface{}{},
	}

	sddlDoc := device.SDDLDocument()
	if sddlDoc != nil {
		out["var_decls"] = sddlDoc.Json()
	}

	outDoc := device.SDDLDocument()
	if outDoc != nil {
		// get most recent value of each sensor/control
		for _, varDef := range outDoc.VarDefs() {
			sample, err := device.LatestDataByName(varDef.Name())
			if err != nil {
				continue
			}
			if timestamp_type == "epoch_us" {
				out["vars"].(map[string]interface{})[varDef.Name()] = map[string]interface{}{
					"t": canotime.EpochMicroseconds(sample.Timestamp),
					"v": sample.Value,
				}
			} else {
				out["vars"].(map[string]interface{})[varDef.Name()] = map[string]interface{}{
					"t": canotime.RFC3339(sample.Timestamp),
					"v": sample.Value,
				}
			}
		}

		// Generate JSON for notifications
		//
		/*notifications, err := device.HistoricNotifications()
		  canolog.Info("Reading notifications")
		  if err != nil {
		      canolog.Info("Error reading notifications %s", err)
		      return nil, err
		  }

		  outNotifications := []jsonNotification{};
		  for _, notification := range notifications {
		      outNotifications = append(
		              outNotifications,
		              jsonNotification{
		                  notification.Datetime().Format(time.RFC3339),
		                  notification.IsDismissed(),
		                  notification.Msg(),
		              })
		  }*/
	}

	return out, nil

}