Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions cmd/client/api/action.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package api

import (
"fmt"
"net/http"

webHandlerTY "github.com/mycontroller-org/server/v2/pkg/types/web_handler"
)

func (c *Client) ExecuteNodeAction(action string, nodeIDs []string) error {
_, err := c.executeJson(API_ACTION_NODE, http.MethodGet, nil, nil, nodeIDs, http.StatusOK)
func (c *Client) ExecuteNodeAction(action string, ids []string) error {
return c.executeIDAction(API_ACTION_NODE, action, ids)
}

func (c *Client) ExecuteGatewayAction(action string, ids []string) error {
return c.executeIDAction(API_ACTION_GATEWAY, action, ids)
}

func (c *Client) executeIDAction(api, action string, ids []string) error {
if action == "" {
return fmt.Errorf("action is required")
}
if len(ids) == 0 {
return fmt.Errorf("at least one id is required")
}
query := map[string]interface{}{
"action": action,
"id": ids,
}
_, err := c.executeJson(api, http.MethodGet, nil, query, nil, http.StatusOK)
return err
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/client/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const (
API_NODE_LIST = "/api/node"
API_NODE_DELETE = "/api/node"

API_ACTION = "/api/action"
API_ACTION_NODE = "/api/action/node"
API_ACTION = "/api/action"
API_ACTION_NODE = "/api/action/node"
API_ACTION_GATEWAY = "/api/action/gateway"

API_FIELD_LIST = "/api/field"
API_FIELD_DELETE = "/api/field"
Expand All @@ -25,6 +26,7 @@ const (

API_FIRMWARE_LIST = "/api/firmware"
API_FIRMWARE_DELETE = "/api/firmware"
API_FIRMWARE_UPLOAD = "/api/firmware/upload"

API_DATA_REPOSITORY_LIST = "/api/datarepository"
API_DATA_REPOSITORY_DELETE = "/api/datarepository"
Expand Down
280 changes: 280 additions & 0 deletions cmd/client/api/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
package api

import (
"fmt"
"net/http"
"strings"

"github.com/mycontroller-org/server/v2/pkg/json"
"github.com/mycontroller-org/server/v2/pkg/types"
dataRepoTY "github.com/mycontroller-org/server/v2/pkg/types/data_repository"
fieldTY "github.com/mycontroller-org/server/v2/pkg/types/field"
firmwareTY "github.com/mycontroller-org/server/v2/pkg/types/firmware"
nodeTY "github.com/mycontroller-org/server/v2/pkg/types/node"
sourceTY "github.com/mycontroller-org/server/v2/pkg/types/source"
"github.com/mycontroller-org/server/v2/pkg/utils"
httpUtils "github.com/mycontroller-org/server/v2/pkg/utils/http_client_json"
storageTY "github.com/mycontroller-org/server/v2/plugin/database/storage/types"
gwTY "github.com/mycontroller-org/server/v2/plugin/gateway/types"
)

func (c *Client) SaveGateway(gateway *gwTY.Config) error {
return c.saveResource(API_GATEWAY_LIST, gateway)
}

func (c *Client) SaveFirmware(firmware *firmwareTY.Firmware) error {
return c.saveResource(API_FIRMWARE_LIST, firmware)
}

func (c *Client) SaveDataRepository(item *dataRepoTY.Config) error {
return c.saveResource(API_DATA_REPOSITORY_LIST, item)
}

func (c *Client) UploadFirmware(id, filename string) error {
client := httpUtils.New(c.Insecure, "10m")
url := fmt.Sprintf("%s%s/%s", c.ServerAddress, API_FIRMWARE_UPLOAD, id)
_, err := client.ExecuteMultipart(url, http.MethodPost, c.getHeaders(nil), "file", filename, http.StatusOK)
return err
}

func (c *Client) SaveNode(node *nodeTY.Node) error {
return c.saveResource(API_NODE_LIST, node)
}

func (c *Client) SaveSource(source *sourceTY.Source) error {
return c.saveResource(API_SOURCE_LIST, source)
}

func (c *Client) SaveField(field *fieldTY.Field) error {
return c.saveResource(API_FIELD_LIST, field)
}

func (c *Client) GetNode(id string) (*nodeTY.Node, error) {
item := &nodeTY.Node{}
found, err := c.getByID(API_NODE_LIST, id, item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) GetSource(id string) (*sourceTY.Source, error) {
item := &sourceTY.Source{}
found, err := c.getByID(API_SOURCE_LIST, id, item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) GetField(id string) (*fieldTY.Field, error) {
item := &fieldTY.Field{}
found, err := c.getByID(API_FIELD_LIST, id, item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) FindGateway(id string) (*gwTY.Config, error) {
if id == "" {
return nil, nil
}
item := &gwTY.Config{}
found, err := c.findResource(API_GATEWAY_LIST, idFilters(id), item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) FindFirmware(id string) (*firmwareTY.Firmware, error) {
if id == "" {
return nil, nil
}
item := &firmwareTY.Firmware{}
found, err := c.findResource(API_FIRMWARE_LIST, idFilters(id), item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) FindDataRepository(id string) (*dataRepoTY.Config, error) {
if id == "" {
return nil, nil
}
item := &dataRepoTY.Config{}
found, err := c.findResource(API_DATA_REPOSITORY_LIST, idFilters(id), item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) FindNode(id, gatewayID, nodeID string) (*nodeTY.Node, error) {
item := &nodeTY.Node{}
found, err := c.findResource(API_NODE_LIST, idFilters(id), item)
if err != nil {
return nil, err
}
if found {
return item, nil
}
if gatewayID == "" || nodeID == "" {
return nil, nil
}
item = &nodeTY.Node{}
found, err = c.findResource(API_NODE_LIST, []storageTY.Filter{
equalFilter(types.KeyGatewayID, gatewayID),
equalFilter(types.KeyNodeID, nodeID),
}, item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) FindSource(id, gatewayID, nodeID, sourceID string) (*sourceTY.Source, error) {
item := &sourceTY.Source{}
found, err := c.findResource(API_SOURCE_LIST, idFilters(id), item)
if err != nil {
return nil, err
}
if found {
return item, nil
}
if gatewayID == "" || nodeID == "" || sourceID == "" {
return nil, nil
}
item = &sourceTY.Source{}
found, err = c.findResource(API_SOURCE_LIST, []storageTY.Filter{
equalFilter(types.KeyGatewayID, gatewayID),
equalFilter(types.KeyNodeID, nodeID),
equalFilter(types.KeySourceID, sourceID),
}, item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) FindField(id, gatewayID, nodeID, sourceID, fieldID string) (*fieldTY.Field, error) {
item := &fieldTY.Field{}
found, err := c.findResource(API_FIELD_LIST, idFilters(id), item)
if err != nil {
return nil, err
}
if found {
return item, nil
}
if gatewayID == "" || nodeID == "" || sourceID == "" || fieldID == "" {
return nil, nil
}
item = &fieldTY.Field{}
found, err = c.findResource(API_FIELD_LIST, []storageTY.Filter{
equalFilter(types.KeyGatewayID, gatewayID),
equalFilter(types.KeyNodeID, nodeID),
equalFilter(types.KeySourceID, sourceID),
equalFilter(types.KeyFieldID, fieldID),
}, item)
if err != nil || !found {
return nil, err
}
return item, nil
}

func (c *Client) saveResource(api string, body interface{}) error {
_, err := c.executeJson(api, http.MethodPost, nil, nil, body, http.StatusOK)
return err
}

func (c *Client) getByID(api, id string, dest interface{}) (bool, error) {
if id == "" {
return false, nil
}
res, err := c.executeJson(fmt.Sprintf("%s/%s", api, id), http.MethodGet, nil, nil, nil, 0)
if err != nil {
return false, err
}
if res.StatusCode == http.StatusNotFound {
return false, nil
}
if res.StatusCode != http.StatusOK {
// FindOne currently returns 500 when no document matches
if res.StatusCode == http.StatusInternalServerError && strings.Contains(res.StringBody(), "no documents") {
return false, nil
}
return false, fmt.Errorf("failed with status code. [statusCode: %v, body: %s]", res.StatusCode, res.StringBody())
}
if len(res.Body) == 0 {
return false, nil
}
if err := json.Unmarshal(res.Body, dest); err != nil {
return false, err
}
return true, nil
}

func (c *Client) findResource(api string, filters []storageTY.Filter, dest interface{}) (bool, error) {
if len(filters) == 0 {
return false, nil
}
queryParams, err := listQueryParams(filters, 1)
if err != nil {
return false, err
}
result, err := c.listResource(api, queryParams)
if err != nil {
return false, err
}
return decodeFirst(result, dest)
}

func listQueryParams(filters []storageTY.Filter, limit uint64) (map[string]interface{}, error) {
filtersBytes, err := json.Marshal(filters)
if err != nil {
return nil, err
}
return map[string]interface{}{
"limit": limit,
"offset": uint64(0),
"filter": string(filtersBytes),
}, nil
}

func decodeFirst(result *storageTY.Result, dest interface{}) (bool, error) {
if result == nil || result.Data == nil {
return false, nil
}
items, ok := result.Data.([]interface{})
if !ok {
return false, fmt.Errorf("invalid response type:%T", result.Data)
}
if len(items) == 0 {
return false, nil
}
data, ok := items[0].(map[string]interface{})
if !ok {
return false, fmt.Errorf("invalid item type:%T", items[0])
}
if err := utils.MapToStruct(utils.TagNameJSON, data, dest); err != nil {
return false, err
}
return true, nil
}

func idFilters(id string) []storageTY.Filter {
if id == "" {
return nil
}
return []storageTY.Filter{equalFilter(types.KeyID, id)}
}

func equalFilter(key, value string) storageTY.Filter {
return storageTY.Filter{
Key: key,
Value: value,
Operator: storageTY.OperatorEqual,
}
}
Loading