github.com/simpleiot/simpleiot@v0.18.3/frontend/src/Api/Node.elm (about)

     1  module Api.Node exposing
     2      ( Node
     3      , NodeView
     4      , Notification
     5      , copy
     6      , delete
     7      , description
     8      , getBestDesc
     9      , insert
    10      , list
    11      , move
    12      , notify
    13      , postPoints
    14      , typeAction
    15      , typeActionInactive
    16      , typeCanBus
    17      , typeCondition
    18      , typeDb
    19      , typeDevice
    20      , typeFile
    21      , typeGroup
    22      , typeMetrics
    23      , typeModbus
    24      , typeModbusIO
    25      , typeMsgService
    26      , typeNTP
    27      , typeNetworkManager
    28      , typeNetworkManagerConn
    29      , typeNetworkManagerDevice
    30      , typeOneWire
    31      , typeParticle
    32      , typeRule
    33      , typeSerialDev
    34      , typeShelly
    35      , typeShellyIO
    36      , typeSignalGenerator
    37      , typeSync
    38      , typeUpdate
    39      , typeUser
    40      , typeVariable
    41      )
    42  
    43  import Api.Data exposing (Data)
    44  import Api.Point as Point exposing (Point)
    45  import Api.Response as Response exposing (Response)
    46  import Http
    47  import Json.Decode as Decode
    48  import Json.Decode.Pipeline exposing (optional, required)
    49  import Json.Encode as Encode
    50  import Url.Builder
    51  
    52  
    53  typeDevice : String
    54  typeDevice =
    55      "device"
    56  
    57  
    58  typeGroup : String
    59  typeGroup =
    60      "group"
    61  
    62  
    63  typeRule : String
    64  typeRule =
    65      "rule"
    66  
    67  
    68  typeCondition : String
    69  typeCondition =
    70      "condition"
    71  
    72  
    73  typeAction : String
    74  typeAction =
    75      "action"
    76  
    77  
    78  typeActionInactive : String
    79  typeActionInactive =
    80      "actionInactive"
    81  
    82  
    83  typeUser : String
    84  typeUser =
    85      "user"
    86  
    87  
    88  typeMsgService : String
    89  typeMsgService =
    90      "msgService"
    91  
    92  
    93  typeDb : String
    94  typeDb =
    95      "db"
    96  
    97  
    98  typeParticle : String
    99  typeParticle =
   100      "particle"
   101  
   102  
   103  typeShelly : String
   104  typeShelly =
   105      "shelly"
   106  
   107  
   108  typeShellyIO : String
   109  typeShellyIO =
   110      "shellyIo"
   111  
   112  
   113  typeModbus : String
   114  typeModbus =
   115      "modbus"
   116  
   117  
   118  typeModbusIO : String
   119  typeModbusIO =
   120      "modbusIo"
   121  
   122  
   123  typeOneWire : String
   124  typeOneWire =
   125      "oneWire"
   126  
   127  
   128  typeSerialDev : String
   129  typeSerialDev =
   130      "serialDev"
   131  
   132  
   133  typeCanBus : String
   134  typeCanBus =
   135      "canBus"
   136  
   137  
   138  typeVariable : String
   139  typeVariable =
   140      "variable"
   141  
   142  
   143  typeSync : String
   144  typeSync =
   145      "sync"
   146  
   147  
   148  typeSignalGenerator : String
   149  typeSignalGenerator =
   150      "signalGenerator"
   151  
   152  
   153  typeFile : String
   154  typeFile =
   155      "file"
   156  
   157  
   158  typeMetrics : String
   159  typeMetrics =
   160      "metrics"
   161  
   162  
   163  typeNetworkManager : String
   164  typeNetworkManager =
   165      "networkManager"
   166  
   167  
   168  typeNetworkManagerDevice : String
   169  typeNetworkManagerDevice =
   170      "networkManagerDevice"
   171  
   172  
   173  typeNetworkManagerConn : String
   174  typeNetworkManagerConn =
   175      "networkManagerConn"
   176  
   177  
   178  typeNTP : String
   179  typeNTP =
   180      "ntp"
   181  
   182  
   183  typeUpdate : String
   184  typeUpdate =
   185      "update"
   186  
   187  
   188  
   189  -- Node corresponds with Go NodeEdge struct
   190  
   191  
   192  type alias Node =
   193      { id : String
   194      , typ : String
   195      , hash : Int
   196      , parent : String
   197      , points : List Point
   198      , edgePoints : List Point
   199      }
   200  
   201  
   202  type alias NodeView =
   203      { node : Node
   204      , feID : Int
   205      , parentID : String
   206      , hasChildren : Bool
   207      , expDetail : Bool
   208      , expChildren : Bool
   209      , mod : Bool
   210      }
   211  
   212  
   213  type alias NodeMove =
   214      { id : String
   215      , oldParent : String
   216      , newParent : String
   217      }
   218  
   219  
   220  type alias NodeCopy =
   221      { id : String
   222      , newParent : String
   223      , duplicate : Bool
   224      }
   225  
   226  
   227  type alias NodeDelete =
   228      { parent : String
   229      }
   230  
   231  
   232  type alias Notification =
   233      { id : String
   234      , parent : String
   235      , sourceNode : String
   236      , subject : String
   237      , message : String
   238      }
   239  
   240  
   241  decodeList : Decode.Decoder (List Node)
   242  decodeList =
   243      Decode.list decode
   244  
   245  
   246  decode : Decode.Decoder Node
   247  decode =
   248      Decode.succeed Node
   249          |> required "id" Decode.string
   250          |> required "type" Decode.string
   251          |> optional "hash" Decode.int 0
   252          |> required "parent" Decode.string
   253          |> optional "points" (Decode.list Point.decode) []
   254          |> optional "edgePoints" (Decode.list Point.decode) []
   255  
   256  
   257  encode : Node -> Encode.Value
   258  encode node =
   259      Encode.object
   260          [ ( "id", Encode.string node.id )
   261          , ( "type", Encode.string node.typ )
   262          , ( "hash", Encode.int node.hash )
   263          , ( "parent", Encode.string node.parent )
   264          , ( "points", Point.encodeList node.points )
   265          , ( "edgePoints", Point.encodeList node.edgePoints )
   266          ]
   267  
   268  
   269  encodeNotification : Notification -> Encode.Value
   270  encodeNotification not =
   271      Encode.object
   272          [ ( "id", Encode.string not.id )
   273          , ( "parent", Encode.string not.parent )
   274          , ( "sourceNode", Encode.string not.sourceNode )
   275          , ( "subject", Encode.string not.subject )
   276          , ( "message", Encode.string not.message )
   277          ]
   278  
   279  
   280  encodeNodeMove : NodeMove -> Encode.Value
   281  encodeNodeMove nodeMove =
   282      Encode.object
   283          [ ( "id", Encode.string nodeMove.id )
   284          , ( "oldParent", Encode.string nodeMove.oldParent )
   285          , ( "newParent", Encode.string nodeMove.newParent )
   286          ]
   287  
   288  
   289  encodeNodeCopy : NodeCopy -> Encode.Value
   290  encodeNodeCopy nodeCopy =
   291      Encode.object
   292          [ ( "id", Encode.string nodeCopy.id )
   293          , ( "newParent", Encode.string nodeCopy.newParent )
   294          , ( "duplicate", Encode.bool nodeCopy.duplicate )
   295          ]
   296  
   297  
   298  encodeNodeDelete : NodeDelete -> Encode.Value
   299  encodeNodeDelete nodeDelete =
   300      Encode.object
   301          [ ( "parent", Encode.string nodeDelete.parent )
   302          ]
   303  
   304  
   305  description : Node -> String
   306  description d =
   307      case Point.get d.points Point.typeDescription "" of
   308          Just point ->
   309              point.text
   310  
   311          Nothing ->
   312              ""
   313  
   314  
   315  getBestDesc : Node -> String
   316  getBestDesc n =
   317      Point.getBestDesc n.points
   318  
   319  
   320  list :
   321      { token : String
   322      , onResponse : Data (List Node) -> msg
   323      }
   324      -> Cmd msg
   325  list options =
   326      Http.request
   327          { method = "GET"
   328          , headers = [ Http.header "Authorization" <| "Bearer " ++ options.token ]
   329          , url = Url.Builder.absolute [ "v1", "nodes" ] []
   330          , expect = Api.Data.expectJson options.onResponse decodeList
   331          , body = Http.emptyBody
   332          , timeout = Nothing
   333          , tracker = Nothing
   334          }
   335  
   336  
   337  delete :
   338      { token : String
   339      , id : String
   340      , parent : String
   341      , onResponse : Data Response -> msg
   342      }
   343      -> Cmd msg
   344  delete options =
   345      Http.request
   346          { method = "DELETE"
   347          , headers = [ Http.header "Authorization" <| "Bearer " ++ options.token ]
   348          , url = Url.Builder.absolute [ "v1", "nodes", options.id ] []
   349          , expect = Api.Data.expectJson options.onResponse Response.decoder
   350          , body = encodeNodeDelete { parent = options.parent } |> Http.jsonBody
   351          , timeout = Nothing
   352          , tracker = Nothing
   353          }
   354  
   355  
   356  insert :
   357      { token : String
   358      , node : Node
   359      , onResponse : Data Response -> msg
   360      }
   361      -> Cmd msg
   362  insert options =
   363      Http.request
   364          { method = "POST"
   365          , headers = [ Http.header "Authorization" <| "Bearer " ++ options.token ]
   366          , url = Url.Builder.absolute [ "v1", "nodes", options.node.id ] []
   367          , expect = Api.Data.expectJson options.onResponse Response.decoder
   368          , body = options.node |> encode |> Http.jsonBody
   369          , timeout = Nothing
   370          , tracker = Nothing
   371          }
   372  
   373  
   374  postPoints :
   375      { token : String
   376      , id : String
   377      , points : List Point
   378      , onResponse : Data Response -> msg
   379      }
   380      -> Cmd msg
   381  postPoints options =
   382      Http.request
   383          { method = "POST"
   384          , headers = [ Http.header "Authorization" <| "Bearer " ++ options.token ]
   385          , url = Url.Builder.absolute [ "v1", "nodes", options.id, "points" ] []
   386          , expect = Api.Data.expectJson options.onResponse Response.decoder
   387          , body = options.points |> Point.encodeList |> Http.jsonBody
   388          , timeout = Nothing
   389          , tracker = Nothing
   390          }
   391  
   392  
   393  notify :
   394      { token : String
   395      , not : Notification
   396      , onResponse : Data Response -> msg
   397      }
   398      -> Cmd msg
   399  notify options =
   400      Http.request
   401          { method = "POST"
   402          , headers = [ Http.header "Authorization" <| "Bearer " ++ options.token ]
   403          , url = Url.Builder.absolute [ "v1", "nodes", options.not.sourceNode, "not" ] []
   404          , expect = Api.Data.expectJson options.onResponse Response.decoder
   405          , body = options.not |> encodeNotification |> Http.jsonBody
   406          , timeout = Nothing
   407          , tracker = Nothing
   408          }
   409  
   410  
   411  move :
   412      { token : String
   413      , id : String
   414      , oldParent : String
   415      , newParent : String
   416      , onResponse : Data Response -> msg
   417      }
   418      -> Cmd msg
   419  move options =
   420      Http.request
   421          { method = "POST"
   422          , headers = [ Http.header "Authorization" <| "Bearer " ++ options.token ]
   423          , url = Url.Builder.absolute [ "v1", "nodes", options.id, "parents" ] []
   424          , expect = Api.Data.expectJson options.onResponse Response.decoder
   425          , body =
   426              { id = options.id
   427              , oldParent = options.oldParent
   428              , newParent = options.newParent
   429              }
   430                  |> encodeNodeMove
   431                  |> Http.jsonBody
   432          , timeout = Nothing
   433          , tracker = Nothing
   434          }
   435  
   436  
   437  copy :
   438      { token : String
   439      , id : String
   440      , newParent : String
   441      , duplicate : Bool
   442      , onResponse : Data Response -> msg
   443      }
   444      -> Cmd msg
   445  copy options =
   446      Http.request
   447          { method = "PUT"
   448          , headers = [ Http.header "Authorization" <| "Bearer " ++ options.token ]
   449          , url = Url.Builder.absolute [ "v1", "nodes", options.id, "parents" ] []
   450          , expect = Api.Data.expectJson options.onResponse Response.decoder
   451          , body =
   452              { id = options.id
   453              , newParent = options.newParent
   454              , duplicate = options.duplicate
   455              }
   456                  |> encodeNodeCopy
   457                  |> Http.jsonBody
   458          , timeout = Nothing
   459          , tracker = Nothing
   460          }