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

     1  module Components.NodeModbusIO exposing (view)
     2  
     3  import Api.Point as Point
     4  import Components.NodeOptions exposing (NodeOptions, oToInputO)
     5  import Element exposing (..)
     6  import Element.Background as Background
     7  import Element.Border as Border
     8  import Element.Font as Font
     9  import Round
    10  import UI.Icon as Icon
    11  import UI.NodeInputs as NodeInputs
    12  import UI.Style as Style exposing (colors)
    13  import UI.ViewIf exposing (viewIf)
    14  
    15  
    16  view : NodeOptions msg -> Element msg
    17  view o =
    18      let
    19          modbusIOType =
    20              Point.getText o.node.points Point.typeModbusIOType ""
    21  
    22          isClient =
    23              case o.parent of
    24                  Just p ->
    25                      Point.getText p.points Point.typeClientServer "" == Point.valueClient
    26  
    27                  Nothing ->
    28                      False
    29  
    30          isWrite =
    31              modbusIOType
    32                  == Point.valueModbusHoldingRegister
    33                  || modbusIOType
    34                  == Point.valueModbusCoil
    35  
    36          value =
    37              Point.getValue o.node.points Point.typeValue ""
    38  
    39          valueSet =
    40              Point.getValue o.node.points Point.typeValueSet ""
    41  
    42          isRegister =
    43              modbusIOType
    44                  == Point.valueModbusInputRegister
    45                  || modbusIOType
    46                  == Point.valueModbusHoldingRegister
    47  
    48          isReadOnly =
    49              Point.getValue o.node.points Point.typeReadOnly "" == 1
    50  
    51          valueText =
    52              if isRegister then
    53                  String.fromFloat (Round.roundNum 2 value)
    54  
    55              else if value == 0 then
    56                  "off"
    57  
    58              else
    59                  "on"
    60  
    61          valueBackgroundColor =
    62              if valueText == "on" then
    63                  Style.colors.blue
    64  
    65              else
    66                  Style.colors.none
    67  
    68          valueTextColor =
    69              if valueText == "on" then
    70                  Style.colors.white
    71  
    72              else
    73                  Style.colors.black
    74  
    75          disabled =
    76              Point.getBool o.node.points Point.typeDisabled ""
    77      in
    78      column
    79          [ width fill
    80          , Border.widthEach { top = 2, bottom = 0, left = 0, right = 0 }
    81          , Border.color colors.black
    82          , spacing 6
    83          ]
    84      <|
    85          wrappedRow [ spacing 10 ]
    86              [ Icon.io
    87              , text <|
    88                  Point.getText o.node.points Point.typeDescription ""
    89                      ++ ": "
    90              , el [ paddingXY 7 0, Background.color valueBackgroundColor, Font.color valueTextColor ] <|
    91                  text <|
    92                      valueText
    93                          ++ (if isRegister then
    94                                  " " ++ Point.getText o.node.points Point.typeUnits ""
    95  
    96                              else
    97                                  ""
    98                             )
    99              , text <|
   100                  if isClient && isWrite && not isReadOnly && value /= valueSet then
   101                      " (cmd pending)"
   102  
   103                  else
   104                      ""
   105              , viewIf disabled <| text "(disabled)"
   106              ]
   107              :: (if o.expDetail then
   108                      let
   109                          labelWidth =
   110                              150
   111  
   112                          opts =
   113                              oToInputO o labelWidth
   114  
   115                          textInput =
   116                              NodeInputs.nodeTextInput opts "0"
   117  
   118                          numberInput =
   119                              NodeInputs.nodeNumberInput opts "0"
   120  
   121                          onOffInput =
   122                              NodeInputs.nodeOnOffInput opts "0"
   123  
   124                          optionInput =
   125                              NodeInputs.nodeOptionInput opts "0"
   126  
   127                          checkboxInput =
   128                              NodeInputs.nodeCheckboxInput opts "0"
   129  
   130                          counterWithReset =
   131                              NodeInputs.nodeCounterWithReset opts "0"
   132                      in
   133                      [ textInput Point.typeDescription "Description" ""
   134                      , viewIf isClient <| numberInput Point.typeID "ID"
   135                      , numberInput Point.typeAddress "Address"
   136                      , optionInput Point.typeModbusIOType
   137                          "IO type"
   138                          [ ( Point.valueModbusDiscreteInput, "discrete input (r)" )
   139                          , ( Point.valueModbusCoil, "coil (rw)" )
   140                          , ( Point.valueModbusInputRegister, "input register(r)" )
   141                          , ( Point.valueModbusHoldingRegister, "holding register(rw)" )
   142                          ]
   143                      , viewIf (isClient && isWrite) <|
   144                          checkboxInput Point.typeReadOnly "Read only"
   145                      , viewIf isRegister <|
   146                          numberInput Point.typeScale "Scale factor"
   147                      , viewIf isRegister <|
   148                          numberInput Point.typeOffset "Offset"
   149                      , viewIf isRegister <|
   150                          textInput Point.typeUnits "Units" ""
   151                      , viewIf isRegister <|
   152                          optionInput Point.typeDataFormat
   153                              "Data format"
   154                              [ ( Point.valueUINT16, "UINT16" )
   155                              , ( Point.valueINT16, "INT16" )
   156                              , ( Point.valueUINT32, "UINT32" )
   157                              , ( Point.valueINT32, "INT32" )
   158                              , ( Point.valueFLOAT32, "FLOAT32" )
   159                              ]
   160  
   161                      -- This can get a little confusing, but client sets the following:
   162                      --   * coil
   163                      --   * holding register
   164                      -- and the server (device) sets the following
   165                      --   * discrete input
   166                      --   * input register
   167                      -- However, some devices also have read only coils and holding regs.
   168                      -- we can't practically have both the client and server setting a
   169                      -- value.
   170                      , viewIf
   171                          (isClient
   172                              && modbusIOType
   173                              == Point.valueModbusHoldingRegister
   174                              && not isReadOnly
   175                          )
   176                        <|
   177                          numberInput Point.typeValueSet "Value"
   178                      , viewIf
   179                          (isClient
   180                              && modbusIOType
   181                              == Point.valueModbusCoil
   182                              && not isReadOnly
   183                          )
   184                        <|
   185                          onOffInput Point.typeValue Point.typeValueSet "Value"
   186                      , viewIf (not isClient && modbusIOType == Point.valueModbusInputRegister) <|
   187                          numberInput Point.typeValue "Value"
   188                      , viewIf (not isClient && modbusIOType == Point.valueModbusDiscreteInput) <|
   189                          onOffInput Point.typeValue Point.typeValue "Value"
   190                      , viewIf isClient <| checkboxInput Point.typeDisabled "Disabled"
   191                      , counterWithReset Point.typeErrorCount Point.typeErrorCountReset "Error Count"
   192                      , counterWithReset Point.typeErrorCountEOF Point.typeErrorCountEOFReset "EOF Error Count"
   193                      , counterWithReset Point.typeErrorCountCRC Point.typeErrorCountCRCReset "CRC Error Count"
   194                      , NodeInputs.nodeKeyValueInput opts Point.typeTag "Tags" "Add Tag"
   195                      ]
   196  
   197                  else
   198                      []
   199                 )