unit GlobalDump;
  // shown modally

  interface
    uses
      AdsData     (* tAdsBCDField, tAdsStringField; also used by AdsFunc *),
      AdsFunc     (* used by AdsTable *),
      AdsTable    (* tAdsQuery *),
      Buttons     (* tBitBtn *),
      Classes,
      ComCtrls    (* tPageControl, tTabSheet *),
      Controls    (* tDate, ModalResult, tModalResult, mrOK *),
      DB          (* used by AdsTable *),
      Forms       (* tForm *),
      StdCtrls    (* tLabel, tListBox *),
      Windows;

    type
      tfmGlobalDump = class(tForm)
          btnBadData: tBitBtn;
          btnOK:      tBitBtn;
          pgctlGlobalDump: tPageControl;
            tabGlobal: tTabSheet;
              qGetBoardInfo: tAdsQuery;
              lbBoard:         tLabel;
              lbBoardTag:      tLabel;
              lbNArgs:         tLabel;
              lbReceivedLine:  tLabel;
              lbSensor:        tLabel;
              lbSensorInfo:    tLabel;
              lbStoreQuery:    tLabel;
              lbTheLine:       tLabel;
              lbTheStoreQuery: tLabel;
              lbxArgs: tListBox;

        public
          procedure GlobalDump(TheStoreQuery: tAdsQuery);
        end (* tfmGlobalDump *);

    var
      fmGlobalDump: tfmGlobalDump;

  implementation
    uses
      BoardManager            (* fmBoardManager *),
      HC3DM                   (* dmHC3 *),
      // MyConsts               (* nullstring *),
      RRF_StringAndTimeUtils  (* Plural *),
      ObservedAndWM           (* tBoardSlot, GetObservedName, MeasurementInfo2,
                                   tMeasurements2, tObserved, tTableUsed,
                                   tWhatMeasured2 *),
      SysUtils                (* Format, FormatDateTime *);
      {$R *.dfm}

    procedure tfmGlobalDump.GlobalDump(TheStoreQuery: tAdsQuery);
      (* gather data *)

      var
        ThisBoardSlot: tBoardSlot;
        ThisBoardTag: char;
        ThisBoardReboot: tDate;
        I, ThisNArgs, ThisSensorUID: integer;
        TheseMeasurements: tMeasurements2;
        ThisObservedUID: tObserved;
        ThisBoardLabel, ThisBoardSlotName, ThisReceivedLine,
          ThisSensorTypeName: string;
        WM: tWhatMeasured2;

      begin (* tfmGlobalDump.GlobalDump *)
        ModalResult := mrOK;
        with Global.LatestData do
          begin (* Global.LatestData *)
            TheseMeasurements  := Measurements;
            ThisBoardSlot      := BoardSlot;
            ThisBoardSlotName  := BoardSlotName;
            ThisBoardTag       := BoardTag;
            ThisNArgs          := NArgs;
            ThisObservedUID    := TheObservedUID;
            ThisReceivedLine   := ReceivedLine;
            ThisSensorTypeName := SensorTypeName;
            ThisSensorUID      := TheSensorUID;

            with lbBoard do
              begin (* lbBoard *)
                if fmBoardManager.BoardTagRecognized(ThisBoardTag) then
                  with qGetBoardInfo do
                    begin (* qGetBoardInfo *)
                      Close;
                      ParamByName('BoardTag').AsString := ThisBoardTag;
                      Open;
                      ThisBoardLabel  := FieldByName('Label'     ).AsString;
                      ThisBoardReboot := FieldByName('LastReboot').AsDateTime;

                      lbBoard.Caption := Format('%s; last reboot %s',
                                                [ThisBoardLabel,
                                                 FormatDateTime('yyyy-mm-dd', ThisBoardReboot)]);
                      Close
                    end   (* qGetBoardInfo *)

                else
                  Caption := '(not recognized)'
              end   (* lbBoard *);

            lbTheLine.Caption := ThisReceivedLine;
            lbSensorInfo.Caption := Format('#%d (%s in slot %d (%s), observing #%d (%s)',
                                           [ThisSensorUID, ThisSensorTypeName,
                                              ord(ThisBoardSlot), ThisBoardSlotName,
                                              ord(ThisObservedUID), GetObservedName(ThisObservedUID)]);
            lbTheStoreQuery.Caption := TheStoreQuery.SQL.Text;

            lbNArgs.Caption := Plural(ThisNArgs, 'argument');
            lbxArgs.Clear;  //              111111111122222
                            //     123456789012345678901234
            lbxArgs.Items.Text := ' I  what measured  value';
            for I := 1 to ThisNArgs do
              begin (* one argument *)
                WM := WMArg[I];
                with lbxArgs do
                  ItemIndex := Items.Add(Format('%2d %12s (#%d) %.2f',
                                                [I,
                                                 MeasurementInfo2[WM].Name, ord(WM),
                                                 RealArg[I]]))
              end   (* one argument *)
          end   (* Global.LatestData *)
      end   (* tfmGlobalDump.GlobalDump *);

end.
