github.com/simpleiot/simpleiot@v0.18.3/frontend/src/Components/NodeParticle.elm (about)

     1  module Components.NodeParticle exposing (view)
     2  
     3  import Api.Point as Point
     4  import Components.NodeOptions exposing (NodeOptions, oToInputO)
     5  import Dict exposing (Dict)
     6  import Element exposing (..)
     7  import Element.Border as Border
     8  import Element.Font as Font
     9  import Round
    10  import Time
    11  import UI.Icon as Icon
    12  import UI.NodeInputs as NodeInputs
    13  import UI.Style exposing (colors)
    14  import UI.ViewIf exposing (viewIf)
    15  import Utils.Iso8601 as Iso8601
    16  
    17  
    18  view : NodeOptions msg -> Element msg
    19  view o =
    20      let
    21          disabled =
    22              Point.getBool o.node.points Point.typeDisabled ""
    23      in
    24      column
    25          [ width fill
    26          , Border.widthEach { top = 2, bottom = 0, left = 0, right = 0 }
    27          , Border.color colors.black
    28          , spacing 6
    29          ]
    30      <|
    31          wrappedRow [ spacing 10 ]
    32              [ Icon.particle
    33              , text <|
    34                  Point.getText o.node.points Point.typeDescription ""
    35              , viewIf disabled <| text "(disabled)"
    36              ]
    37              :: (if o.expDetail then
    38                      let
    39                          labelWidth =
    40                              150
    41  
    42                          opts =
    43                              oToInputO o labelWidth
    44  
    45                          textInput =
    46                              NodeInputs.nodeTextInput opts "0"
    47  
    48                          checkboxInput =
    49                              NodeInputs.nodeCheckboxInput opts "0"
    50                      in
    51                      [ text "Particle.io connection"
    52                      , textInput Point.typeDescription "Description" ""
    53                      , textInput Point.typeAuthToken "API Key" ""
    54                      , checkboxInput Point.typeDisabled "Disabled"
    55                      , NodeInputs.nodeKeyValueInput opts Point.typeTag "Tags" "Add Tag"
    56                      , viewPoints o.zone <| Point.filterSpecialPoints <| List.sortWith Point.sort o.node.points
    57                      ]
    58  
    59                  else
    60                      []
    61                 )
    62  
    63  
    64  viewPoints : Time.Zone -> List Point.Point -> Element msg
    65  viewPoints z pts =
    66      let
    67          formaters =
    68              metricFormaters z
    69  
    70          fm =
    71              formatMetric formaters
    72      in
    73      table [ padding 7 ]
    74          { data = List.map (fm z) pts
    75          , columns =
    76              let
    77                  cell =
    78                      el [ paddingXY 15 5, Border.width 1 ]
    79              in
    80              [ { header = cell <| el [ Font.bold, centerX ] <| text "Time"
    81                , width = fill
    82                , view = \m -> cell <| text m.time
    83                }
    84              , { header = cell <| el [ Font.bold, centerX ] <| text "ID"
    85                , width = fill
    86                , view = \m -> cell <| text m.key
    87                }
    88              , { header = cell <| el [ Font.bold, centerX ] <| text "Type"
    89                , width = fill
    90                , view = \m -> cell <| text m.desc
    91                }
    92              , { header = cell <| el [ Font.bold, centerX ] <| text "Value"
    93                , width = fill
    94                , view = \m -> cell <| el [ alignRight ] <| text m.value
    95                }
    96              ]
    97          }
    98  
    99  
   100  formatMetric : Dict String MetricFormat -> Time.Zone -> Point.Point -> { time : String, key : String, desc : String, value : String }
   101  formatMetric formaters z p =
   102      case Dict.get p.typ formaters of
   103          Just f ->
   104              { time = Iso8601.toString Iso8601.Second z p.time
   105              , key = p.key
   106              , desc = f.desc p
   107              , value = f.vf p
   108              }
   109  
   110          Nothing ->
   111              { time = Iso8601.toString Iso8601.Second z p.time
   112              , key = p.key
   113              , desc = p.typ
   114              , value = toOneDec p
   115              }
   116  
   117  
   118  type alias MetricFormat =
   119      { desc : Point.Point -> String
   120      , vf : Point.Point -> String
   121      }
   122  
   123  
   124  metricFormaters : Time.Zone -> Dict String MetricFormat
   125  metricFormaters _ =
   126      Dict.fromList
   127          [ ( "temp", { desc = descS "Temperature", vf = toOneDec } )
   128          ]
   129  
   130  
   131  descS : String -> Point.Point -> String
   132  descS d _ =
   133      d
   134  
   135  
   136  toOneDec : Point.Point -> String
   137  toOneDec p =
   138      Round.round 1 p.value