爱招飞帮助手册 爱招飞帮助手册
首页
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 系统工具
    • 系统管理
    • 数据库工具
    • 专用模板
    • 外部功能
    • 开发流程
    • 函数代码
  • 开发手册
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 开发手册
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 开发手册
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • 报表设计
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • RestAPI
    • WebSocket
  • 学习手册

    • 安装配置
    • 快速上手
    • 程序架构
    • 界面介绍
    • 功能介绍
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • 预设资料
    • RestAPI说明
    • WebSocket说明
    • MQTT说明
    • Python说明
    • 自定程序
    • 运行卫士
    • 自动化作业
    • 函数程序
    • 控件使用
  • 开发手册
  • FastERP
  • FastWeb
  • Smart
  • PinToo
  • Flying
  • TARS
  • 通用功能

    • Report
    • Script
    • Echarts
    • Chart
    • DB Install
    • HiDesk
    • HiNAT
    • FastBPM
    • ReportBuilder
首页
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 系统工具
    • 系统管理
    • 数据库工具
    • 专用模板
    • 外部功能
    • 开发流程
    • 函数代码
  • 开发手册
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 开发手册
  • 学习手册

    • 基本入门
    • 功能介绍
    • 控件说明
    • 功能用法
    • 专用模板
    • 开发流程
    • 函数程序
  • 开发手册
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • 报表设计
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • RestAPI
    • WebSocket
  • 学习手册

    • 安装配置
    • 快速上手
    • 程序架构
    • 界面介绍
    • 功能介绍
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
  • 学习手册

    • 安装配置
    • 快速上手
    • 界面介绍
    • 功能介绍
    • 预设资料
    • RestAPI说明
    • WebSocket说明
    • MQTT说明
    • Python说明
    • 自定程序
    • 运行卫士
    • 自动化作业
    • 函数程序
    • 控件使用
  • 开发手册
  • FastERP
  • FastWeb
  • Smart
  • PinToo
  • Flying
  • TARS
  • 通用功能

    • Report
    • Script
    • Echarts
    • Chart
    • DB Install
    • HiDesk
    • HiNAT
    • FastBPM
    • ReportBuilder
  • PinToo帮助主页
  • 学习手册

  • 开发手册

    • 通讯协议

      • 串口

      • 蓝牙Bluetooth

      • 无线网WiFi

      • ModbusRTU

      • ModbusTCP

      • MQTT

      • RestAPI

      • HTTP

        • 短信发送
        • 自定HTTP Server服务器
          • 1. 说明
          • 2. 设计明细
          • 3. 程序设计
            • 3.1. 程序初始设置
            • 3.2. 事件设置
          • 4. 运行结果
        • 自定HTTP Client客户端
    • 交互输出

    • 媒体存取

    • 人工智慧

    • 实例运用

目录
1. 说明
2. 设计明细
3. 程序设计
3.1. 程序初始设置
3.2. 事件设置
4. 运行结果

自定HTTP Server服务器

# PinToo之自定HTTP Server服务器

# 1. 说明

  通过PinToo智慧控制平台,实现接收客户端请求并向TARS服务端发起连接。该示例可与PinToo之自定HTTP Client客户端示例配合使用。该功能需要启用TARS连接。在使用以下功能之前,请先确认是否有给予PinToo相应的权限。

  该范例使用的是类rest风格的请求方式,rest api 是前后端分离最佳实践,是开发的一套标准或者说是一套规范。其优点如下:

  1. 轻量,直接通过http,不需要额外的协议,通常有post/get/put/delete操作。
  2. 面向资源,一目了然,具有自解释性。
  3. 数据描述简单,一般通过json或者xml做数据通讯。

  在使用该范例之前,请先创建一个名为test的数据库,并在其中创建一个Table_Test表,创建语句示例如下:

--创建数据表 Table_Test
Create Table Table_Test(
FCode nvarchar(50) not null,
FName nvarchar(100)
)
GO
--创建表主索引 PK_Table_Test
CREATE UNIQUE NONCLUSTERED INDEX [PK_Table_Test] ON [dbo].[Table_Test] 
([FCode] ASC)
WITH (PAD_INDEX  = OFF, 
STATISTICS_NORECOMPUTE  = OFF, 
SORT_IN_TEMPDB = OFF, 
IGNORE_DUP_KEY = OFF, 
DROP_EXISTING = OFF, 
ONLINE = OFF, 
ALLOW_ROW_LOCKS  = ON, 
ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

--插入测试用的数据
INSERT INTO Table_Test(FCode,FName)
VALUES('0101','小明')
 
INSERT INTO Table_Test(FCode,FName)
VALUES('0102','小王')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

  在PinToo登录时配置服务端的地址,端口号和连接的数据库连接设置名称。

  通过范例学习,可以掌握fxIdHTTPServer的基本使用方式。

# 2. 设计明细

  开启PinToo设计器,分别加入如下插图之控件。或者点击左上角的[打开模板Lib文件],选择模板文件来打开对应模板。

自定HTTPServer服务器 UI布局

  ①:TfxSuperButton组件,控件名称为fxSuperButtonStart。

  ②:TfxEdit组件,控件名称为fxEditPort。

  ③:TfxIdHTTPServer组件,控件名称为fxIdHTTPServer1。

  ④:TfxRFDataSet组件,控件名称为fxRFDataSet1。

  ⑤:TfxDataSource组件,控件名称为fxDataSource1。

  ⑥:TfxSuperButton组件,控件名称为fxSuperButtonStop。

  ⑦:TfxGridView组件,控件名称为fxGridView1。

  ⑧:TfxSwitchButton组件,控件名称为fxSwitchButton1。

  ⑨:TfxMemo组件,控件名称为fxMemo1。

  • fxRunFrame属性设置

    • Height:设置页面高度=800。
    • Width:设置页面宽度=400。
  • ①fxSuperButtonStart属性设置

    • Height:设置控件高度=80。
    • Width:设置控件宽度=80。
    • ButtonType:设置按钮类型,设置为TfxSvgButton。
    • Name:设置控件名称=fxSuperButtonStart。
    • ButtonType.SvgData:设置SVG数据,其数据内容如下。
    <path d="M14.66588306427,41.3929443359375 L14.66588306427,41.3929443359375 L14.66588306427,41.3929443359375 L14.8400011062622,41.3929443359375 L14.66588306427,41.3929443359375 Z" p-id="7305" fill="#FF2C2C2C" stroke="Null"></path>
    <path d="M66.0305938720703,13.8823585510254 L66.0305938720703,13.8823585510254 C59.2400016784668,7.09177255630493 50.0117683410645,3.08706665039063 39.9129409790039,3.08706665039063 C29.8141193389893,3.08706665039063 20.4117641448975,7.26589012145996 13.7952938079834,13.8823585510254 L13.7952938079834,13.8823585510254 C7.17882347106934,20.4988307952881 3,29.7270603179932 3,40.0000038146973 C3,50.0988349914551 7.17882347106934,59.5011863708496 13.7952938079834,66.1176452636719 C20.4117641448975,72.9082336425781 29.8141136169434,76.9129333496094 39.9129409790039,76.9129333496094 C50.1858825683594,76.9129333496094 59.414119720459,72.7341156005859 66.0305938720703,66.1176452636719 C72.821174621582,59.5011863708496 76.8258819580078,50.27294921875 76.8258819580078,40.0000038146973 C77,29.7270603179932 72.821174621582,20.4988307952881 66.0305938720703,13.8823585510254 Z M62.0258827209473,62.1129379272461 C56.4541130065918,67.6847076416016 48.6188201904297,71.3411560058594 39.9129371643066,71.3411560058594 C31.2070598602295,71.3411560058594 23.3717632293701,67.8588104248047 17.7999992370605,62.1129379272461 C12.2282361984253,56.5411758422852 8.5717658996582,48.7058868408203 8.5717658996582,40.0000038146973 C8.5717658996582,31.2941226959229 12.0541191101074,23.4588317871094 17.7999992370605,17.8870658874512 C23.5458831787109,12.3153009414673 31.3811779022217,8.6588306427002 39.9129409790039,8.6588306427002 C48.4447059631348,8.6588306427002 56.2799987792969,12.1411838531494 61.8517646789551,17.7129516601563 L62.0258827209473,17.8870658874512 C67.5976486206055,23.4588317871094 71.2541198730469,31.2941226959229 71.2541198730469,40.0000038146973 C71.2541198730469,48.7058944702148 67.7717666625977,56.5411758422852 62.0258827209473,62.1129379272461 Z" p-id="7306" fill="#FF2C2C2C" stroke="Null"></path>
    <path d="M29.2917633056641,17.8870658874512 L46.529411315918,27.8117733001709 L63.7670593261719,37.7364730834961 C64.9858856201172,38.4329414367676 65.3341217041016,40.0000038146973 64.6376495361328,41.2188377380371 C64.2894134521484,41.567066192627 63.9411735534668,41.9153099060059 63.5929412841797,42.2635345458984 L46.3552894592285,52.1882514953613 L46.3552894592285,52.1882514953613 L29.1176433563232,62.1129531860352 C27.8988189697266,62.8094215393066 26.3317604064941,62.4611892700195 25.6352920532227,61.2423629760742 C25.4611721038818,60.7200126647949 25.2870559692383,60.3717765808105 25.2870559692383,59.849437713623 L25.2870616912842,40.0000038146973 L25.2870616912842,40.0000038146973 L25.2870616912842,19.9764785766602 C25.2870616912842,18.5835380554199 26.3317642211914,17.5388298034668 27.7247085571289,17.5388298034668 C28.4211750030518,17.5388298034668 28.9435329437256,17.7129516601563 29.2917633056641,17.8870658874512 L29.2917633056641,17.8870658874512 Z" p-id="7307" fill="#FF2C2C2C" stroke="Null"></path>
    
    1
    2
    3
  • ②fxEditPort属性设置

    • Height:设置控件高度=30。
    • Width:设置控件宽度=300。
    • Text:设置编辑框文本,此处设置为端口号=8806。
    • KeyBoardType:设置显示键盘的类型=NumberPad。
  • ⑤fxDataSource属性设置

    • DataSource:定义数据源,设置为fxDataSource1。
  • ⑥fxSuperButtonStop属性设置

    • Height:设置控件高度=80。
    • Width:设置控件宽度=80。
    • ButtonType:设置按钮类型,设置为TfxSvgButton。
    • Name:设置控件名称=fxSuperButtonStop。
    • ButtonType.SvgData:设置SVG数据,其数据内容如下。
    <path d="M40,77 C19.6499862670898,77 3,60.3500022888184 3,40 C3,19.6499977111816 19.6499862670898,3 40,3 C60.3500099182129,3 77,19.6499977111816 77,40 C77,60.3500022888184 60.3500099182129,77 40,77 Z M40,72.375 C58.0375099182129,72.375 72.375,58.0374946594238 72.375,40 C72.375,21.9625034332275 58.0375099182129,7.625 40,7.625 C21.9624862670898,7.625 7.625,21.9624977111816 7.625,40 C7.625,58.0374946594238 21.9624862670898,72.375 40,72.375 Z" p-id="6408" fill="#FF333333" stroke="Null"></path>
    <path d="M26.125,26.1250076293945 L53.875,26.1250076293945 L53.875,53.8750152587891 L26.125,53.8750152587891 Z" p-id="6409" fill="#FF333333" stroke="Null"></path>
    
    1
    2
  • ⑦fxGridView1属性设置

    • Height:设置控件高度=205。
    • Width:设置控件宽度=360。
    • DataSource:定义数据源,此处设置为fxDataSource1。
  • ⑧fxSwitchButton1属性设置

    • Height:设置控件高度=50。
    • Width:设置控件宽度=50。
  • ⑨fxMemo1属性设置

    • Height:设置控件高度=190。
    • Width:设置控件宽度=360。
    • Opacity:设置透控件明度=0.9。

# 3. 程序设计

  点击设计界面右下角的按钮,切换至单元选择界面,勾选需要使用的单元。程序需引用IdCustomHTTPServer,IdHTTPServer,ufxDm,ufxFunctions,TARSCompClient单元。

# 3.1. 程序初始设置

  该程序无初始设置。

# 3.2. 事件设置

  • ①fxSuperButtonStart-OnClick事件

  点击启动按钮时开启HTTP Server服务。

Procedure fxSuperButtonStartOnClick(Sender: TObject);
//启动HTTP服务
Begin
  if not fxIdHTTPServer1.Active then
  begin
    try
      fxIdHTTPServer1.Bindings.Clear;
      fxIdHTTPServer1.DefaultPort := StrToInt(fxEditPort.Text);
      fxIdHTTPServer1.Active := True;
      fxMemo1.Lines.Insert(0, FormatDateTime('HH:NN:SS.ZZZ ', now) 
      + 'HTTP服务 已启动。')
    except
      fxMemo1.Lines.Insert(0, FormatDateTime('HH:NN:SS.ZZZ ', now) 
      + 'HTTP服务 启动失败。')
    end;
  end;
End;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • ⑥fxSuperButtonStop-OnClick事件

  点击停止按钮时关闭HTTP Server服务。

Procedure fxSuperButtonStopOnClick(Sender: TObject);
//停止HTTP服务
Begin
  fxIdHTTPServer1.Active := False;
  fxIdHTTPServer1.Bindings.Clear;
  fxMemo1.Lines.Insert(0, FormatDateTime('HH:NN:SS.ZZZ ', now) 
  + 'HTTP服务 已停止。');
End;
1
2
3
4
5
6
7
8
  • ③fxIdHTTPServer1-OnCommandGet事件

  当服务器获取到命令时,向TARS发起数据库链接请求。

Procedure fxIdHTTPServer1OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
//获取HTTP请求并向TARS服务器发起数据库连接请求
var
  ReportType:String;
  APathInfo,SQL:string;
  str:String;
  Stream: TStringStream;
begin
  Stream := TStringStream.Create;
  if ARequestInfo.Command='POST' then
  begin
    AResponseInfo.CustomHeaders.Add('Access-Control-Allow-Origin:*');
    AResponseInfo.CustomHeaders.Add('Access-Control-Allow-Headers:*');
    AResponseInfo.CustomHeaders.Add('Access-Control-Allow-Method:*');
 
    APathInfo := ARequestInfo.Document;
 
    if APathInfo = '/opensql' then
    begin
       Stream.LoadFromStream(ARequestInfo.PostStream);
       SQL := Stream.DataString;
       if SQL='' then
       begin
        str := ('{"status":"error","data":"SQL内容不能为空!"}');
        fxMemo1.Lines.Insert(0, FormatDateTime('HH:NN:SS.ZZZ ', now) 
        + 'SQL内容不能为空!');
        AResponseInfo.ContentText := fxfun.URLEncode(str);
        AResponseInfo.WriteContent;
        Exit;
       end;
   
       fxRFDataSet1.OpenSQL(SQL);
       fxMemo1.Lines.Insert(0, FormatDateTime('HH:NN:SS.ZZZ ', now) 
       + '接收到的SQL内容:'+SQL);
       //返回查询数据
       str := ('{"status":"ok", ' + fxdm.DataSetToStr(fxRFDataSet1,'DataSet') + '}');
       AResponseInfo.ContentText := fxfun.URLEncode(str);
       AResponseInfo.WriteContent;
    end;
  end;
  Stream.Free;
end;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  • ⑧fxSwitchButton1-OnSwitch事件

  点击切换以开启/关闭日志框。

Procedure fxSwitchButton1OnSwitch(Sender: TObject);
//日志显示
Begin
  fxMemo1.Visible := fxSwitchButton1.IsChecked;
End;
1
2
3
4
5

# 4. 运行结果

  使用鼠标在 PinToo 菜单,点击[保存至数据库]按钮,将其保存至数据库,点击[调试运行]确认能够正常打开。

自定HTTPServer 运行效果图

  通过同步中心,将程序上传至手机PinToo运行;同步时,请确保手机已经运行PinToo,并且已经登陆。

  使用自定HTTP Client客户端对该服务器进行访问。首先设定端口号,点击启动按钮,同时可打开日志按钮查看程序运行日志。在客户端处发起SQL查询的访问,在服务器端显示查询的结果,同时返回查询结果的数据集JSON。

短信发送
自定HTTP Client客户端

← 短信发送 自定HTTP Client客户端→

Copyright © 2021-2025 爱招飞IsoFace | ALL Rights Reserved
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式