Skip to content

Commit

Permalink
Add HTTP Send and WS Listen
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
  • Loading branch information
darkodraskovic committed Apr 26, 2019
1 parent 66f3dad commit 966731d
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 130 deletions.
67 changes: 2 additions & 65 deletions ui/src/Connection.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- SPDX-License-Identifier: Apache-2.0


port module Connection exposing (Model, Msg(..), initial, subscriptions, update, view)
port module Connection exposing (Model, Msg(..), initial, update, view)

import Bootstrap.Button as Button
import Bootstrap.Card as Card
Expand Down Expand Up @@ -39,7 +39,6 @@ type alias Model =
, things : Thing.Model
, channels : Channel.Model
, checkedThingsIds : List String
, checkedThingsKeys : List String
, checkedChannelsIds : List String
, websocketIn : List String
}
Expand All @@ -51,7 +50,6 @@ initial =
, things = Thing.initial
, channels = Channel.initial
, checkedThingsIds = []
, checkedThingsKeys = []
, checkedChannelsIds = []
, websocketIn = []
}
Expand All @@ -60,9 +58,6 @@ initial =
type Msg
= Connect
| Disconnect
| Listen
| WebsocketIn String
| Stop
| ThingMsg Thing.Msg
| ChannelMsg Channel.Msg
| GotResponse (Result Http.Error String)
Expand All @@ -72,7 +67,7 @@ type Msg

resetChecked : Model -> Model
resetChecked model =
{ model | checkedThingsIds = [], checkedThingsKeys = [], checkedChannelsIds = [] }
{ model | checkedThingsIds = [], checkedChannelsIds = [] }


isEmptyChecked : Model -> Bool
Expand Down Expand Up @@ -101,28 +96,6 @@ update msg model token =
, Cmd.batch (connect model.checkedThingsIds model.checkedChannelsIds "DELETE" token)
)

Listen ->
if isEmptyChecked model then
( model, Cmd.none )

else
( resetChecked model
, Cmd.batch
(connect model.checkedThingsIds model.checkedChannelsIds "PUT" token
++ ws connectWebsocket model.checkedThingsKeys model.checkedChannelsIds
)
)

Stop ->
if isEmptyChecked model then
( resetChecked model, Cmd.none )

else
( resetChecked model, Cmd.batch (ws disconnectWebsocket model.checkedThingsKeys model.checkedChannelsIds) )

WebsocketIn data ->
( { model | websocketIn = data :: model.websocketIn }, Cmd.none )

GotResponse result ->
case result of
Ok statusCode ->
Expand All @@ -148,7 +121,6 @@ update msg model token =
CheckThing thing ->
( { model
| checkedThingsIds = Helpers.checkEntity (Tuple.first thing) model.checkedThingsIds
, checkedThingsKeys = Helpers.checkEntity (Tuple.second thing) model.checkedThingsKeys
}
, Cmd.none
)
Expand All @@ -158,17 +130,6 @@ update msg model token =



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ websocketIn WebsocketIn
]



-- VIEW


Expand All @@ -194,15 +155,8 @@ view model =
, Button.button [ Button.danger, Button.attrs [ Spacing.ml1 ], Button.onClick Disconnect ] [ text "Disconnect" ]
]
]
, Grid.col [ Col.attrs [ align "right" ] ]
[ Form.form []
[ Button.button [ Button.success, Button.attrs [ Spacing.ml1 ], Button.onClick Listen ] [ text "WS Listen" ]
, Button.button [ Button.danger, Button.attrs [ Spacing.ml1 ], Button.onClick Stop ] [ text "WS Stop" ]
]
]
]
, Helpers.response model.response
, Helpers.genOrderedList model.websocketIn
]


Expand Down Expand Up @@ -255,20 +209,3 @@ connect checkedThingsIds checkedChannelsIds method token =
)
checkedThingsIds
)


ws : (E.Value -> Cmd Msg) -> List String -> List String -> List (Cmd Msg)
ws command checkedThingsKeys checkedChannelsIds =
List.foldr (++)
[]
(List.map
(\thingkey ->
List.map
(\channelid ->
command <|
websocketEncoder (Websocket channelid thingkey "")
)
checkedChannelsIds
)
checkedThingsKeys
)
2 changes: 2 additions & 0 deletions ui/src/Helpers.elm
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ faIcons =
, messages = "far fa-paper-plane"
, version = "fa fa-code-branch"
, websocket = "fas fa-arrows-alt-v"
, send = "fas fa-arrow-up"
, receive = "fas fa-arrow-down"
}


Expand Down
1 change: 0 additions & 1 deletion ui/src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Sub.map UserMsg (User.subscriptions model.user)
, Sub.map ConnectionMsg (Connection.subscriptions model.connection)
, Sub.map MessageMsg (Message.subscriptions model.message)
]

Expand Down
146 changes: 100 additions & 46 deletions ui/src/Message.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Bootstrap.Form.Checkbox as Checkbox
import Bootstrap.Form.Input as Input
import Bootstrap.Form.Radio as Radio
import Bootstrap.Grid as Grid
import Bootstrap.Grid.Col as Col
import Bootstrap.Table as Table
import Bootstrap.Utilities.Spacing as Spacing
import Channel
Expand Down Expand Up @@ -69,7 +70,8 @@ initial =
type Msg
= SubmitMessage String
| SendMessage
| WebsocketSend
| Listen
| Stop
| WebsocketIn String
| RetrievedWebsockets E.Value
| SentMessage (Result Http.Error String)
Expand Down Expand Up @@ -99,17 +101,19 @@ update msg model token =
)
)

WebsocketSend ->
( model
, Cmd.batch
(List.map
(\channelid ->
websocketOut <|
websocketEncoder (Websocket channelid model.thingkey model.message)
)
model.checkedChannelsIds
)
)
Listen ->
if List.isEmpty model.checkedChannelsIds then
( model, Cmd.none )

else
( model, Cmd.batch <| ws connectWebsocket model )

Stop ->
if List.isEmpty model.checkedChannelsIds then
( model, Cmd.none )

else
( model, Cmd.batch <| ws disconnectWebsocket model )

SentMessage result ->
case result of
Expand All @@ -125,15 +129,19 @@ update msg model token =
RetrievedWebsockets wssList ->
case D.decodeValue websocketsQueryDecoder wssList of
Ok wssL ->
let
l =
List.map
(\wss ->
channelIdFromUrl wss.url
)
wssL
in
( { model | checkedChannelsIdsWs = log "wssList" l }, Cmd.none )
if List.isEmpty wssL then
( model, Cmd.none )

else
let
l =
List.map
(\wss ->
channelIdFromUrl wss.url
)
wssL
in
( { model | checkedChannelsIdsWs = l }, Cmd.none )

Err _ ->
( model, Cmd.none )
Expand All @@ -151,13 +159,28 @@ update msg model token =
( { model | checkedChannelsIds = Helpers.checkEntity id model.checkedChannelsIds }, Cmd.none )


retrieveWebsocketsForThing : List Channel.Channel -> String -> Cmd Msg
retrieveWebsocketsForThing channels thingkey =
let
wssList =
List.map
(\channel ->
Websocket channel.id thingkey ""
)
channels
in
queryWebsockets (websocketsEncoder wssList)


updateThing : Model -> Thing.Msg -> String -> ( Model, Cmd Msg )
updateThing model msg token =
let
( updatedThing, thingCmd ) =
Thing.update msg model.things token
in
( { model | things = updatedThing }, Cmd.map ThingMsg thingCmd )
( { model | things = updatedThing }
, Cmd.map ThingMsg thingCmd
)


updateChannel : Model -> Channel.Msg -> String -> ( Model, Cmd Msg )
Expand All @@ -169,22 +192,9 @@ updateChannel model msg token =
checkedChannels =
updatedChannel.channels.list
in
case msg of
Channel.RetrievedChannels _ ->
let
wssList =
List.map
(\channel ->
Websocket channel.id model.thingkey ""
)
checkedChannels
in
( { model | channels = updatedChannel }
, queryWebsockets (websocketsEncoder wssList)
)

_ ->
( { model | channels = updatedChannel }, Cmd.map ChannelMsg channelCmd )
( { model | channels = updatedChannel }
, Cmd.map ChannelMsg channelCmd
)



Expand Down Expand Up @@ -221,23 +231,58 @@ view model =
, Grid.row []
[ Grid.col []
[ Card.config []
|> Card.headerH3 [] [ div [ class "table_header" ] [ i [ style "margin-right" "15px", class faIcons.messages ] [], text "Message" ] ]
|> Card.block []
[ Block.custom
(Form.form []
|> Card.headerH3 []
[ Grid.row []
[ Grid.col []
[ div [ class "table_header" ]
[ i [ style "margin-right" "15px", class faIcons.send ] []
, text "HTTP"
]
]
, Grid.col [ Col.attrs [ align "right" ] ]
[ Form.group []
[ Input.text [ Input.id "message", Input.onInput SubmitMessage ]
[ Button.button [ Button.secondary, Button.attrs [ Spacing.ml1 ], Button.onClick SendMessage ] [ text "Send" ]
]
, Button.button [ Button.secondary, Button.attrs [ Spacing.ml1 ], Button.onClick SendMessage ] [ text "Send" ]
, Button.button [ Button.secondary, Button.attrs [ Spacing.ml1 ], Button.onClick WebsocketSend ] [ text "Websocket" ]
]
]
]
|> Card.block []
[ Block.custom
(Grid.row []
[ Grid.col [] [ Input.text [ Input.id "message", Input.onInput SubmitMessage ] ]
]
)
]
|> Card.view
]
, Grid.col []
[ Card.config []
|> Card.headerH3 []
[ Grid.row []
[ Grid.col []
[ div [ class "table_header" ]
[ i [ style "margin-right" "15px", class faIcons.receive ] []
, text "WS"
]
]
, Grid.col [ Col.attrs [ align "right" ] ]
[ Form.form []
[ Form.group []
[ Button.button [ Button.secondary, Button.attrs [ Spacing.ml1 ], Button.onClick Listen ] [ text "Listen" ]
, Button.button [ Button.secondary, Button.attrs [ Spacing.ml1 ], Button.onClick Stop ] [ text "Stop" ]
]
]
]
]
]
|> Card.block []
[ Block.custom
(Helpers.genOrderedList model.websocketIn)
]
|> Card.view
]
]
, Helpers.response model.response
, Helpers.genOrderedList model.websocketIn
]


Expand Down Expand Up @@ -288,3 +333,12 @@ send channelid thingkey message =
thingkey
(Http.stringBody "application/json" message)
SentMessage


ws : (E.Value -> Cmd Msg) -> Model -> List (Cmd Msg)
ws command model =
List.map
(\channelid ->
command <| websocketEncoder (Websocket channelid model.thingkey "")
)
model.checkedChannelsIds
Loading

0 comments on commit 966731d

Please sign in to comment.