⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsmcom.pas

📁 很好的手机发短信的例子。含GSM群发机设计原理和使用说明。还有详细代码
💻 PAS
📖 第 1 页 / 共 5 页
字号:

var
  TCount: Integer;
  ATask: TGSMTask;
  K: Integer;
  TaskSection: String;
  CDST: TClientDataSet;
  TheTaskCount: Integer;
  CurCount: Integer;
begin
  TCount := IniFile.ReadInteger('Setting','TaskCount',0);
  for k:=0 to TCount do
  begin
    TaskSection := 'Task'+IntToStr(k);
    IniFile.EraseSection(TaskSection);
  end;
  TheTaskCount := 0;
  for k:= 0 to Count-1 do
    if not Task[k].isFix then
      TheTaskCount := TheTaskCount + 1;


  IniFile.WriteInteger('Setting','TaskCount',TheTaskCount);

  CDST := CreateListCDST;

  CurCount := 0;
  for k:=0 to Count-1 do
  begin
    ATask := Task[k];
    if not ATask.isFix then
    begin
      TaskSection := 'Task'+IntToStr(CurCount);
      IniFile.WriteString(TaskSection,'TaskName',ATask.TaskName);
      IniFile.WriteDateTime(TaskSection,'TaskTime',ATask.TaskTime);
      IniFile.WriteDateTime(TaskSection,'TaskStartTime',ATask.TaskStartTime);
      IniFile.writeString(TaskSection,'Message',ATask.SMSMessage);
      IniFile.WriteBool(TaskSection,'DynamicDataBase',ATask.DynamicDataBase);
      IniFile.WriteInteger(TaskSection,'DynamicCount',ATask.DataBaseInfo.UpdateCount);
      case  ATask.DataBaseInfo.UpdateType of
        utDelete:
          IniFile.WriteInteger(TaskSection,'DynamicType',1);
        utUpdate:
          IniFile.WriteInteger(TaskSection,'DynamicType',0);
        else
          IniFile.WriteInteger(TaskSection,'DynamicType',0);
      end;

      IniFile.WriteInteger(TaskSection,'SendedCount',ATask.SendedCount);

      IniFile.writeString(TaskSection,'ConnectionString',ATask.DataBaseInfo.ConnectionString);
      IniFile.WriteString(TaskSection,'TableName',ATask.DataBaseInfo.TableName);
      IniFile.WriteString(TaskSection,'FieldName',ATask.DataBaseInfo.FieldName);
      IniFile.WriteString(TaskSection,'KeyFieldName',ATask.DataBaseInfo.KeyFieldName);
      IniFile.WriteString(TaskSection,'StateFieldName',ATask.DataBaseInfo.StateFieldName);
      IniFile.WriteString(TaskSection,'MSGFieldName',ATask.DataBaseInfo.MSGFieldName);
      IniFile.WriteString(TaskSection,'WaitCDSTName',ATask.WaitCDSTName);
      try
        SaveItemsToCDST(CDST,ATask.WaitSendItems,ATask.WaitCDSTName,ATask);
      except
      end;

      Inc(CurCount);
    end;
  end;
  CDST.Free;
end;

procedure TGSMTasks.LoadFromIniFile(IniFile: TIniFile);

  procedure LoadItemsFromCDST(TheCDST: TClientDataSet;
    TheSendItems: TSendItems; FileName: String; TheTask: TGSMTask);
  var
    AMessageSendItem: TGSMMessageItem;
    ADialSendItem: TGSMDialItem;
    ASendItem: TGSMSendItem;
    exFileName: String;
  begin
    if FileName <>'' then
    begin
      TheCDST.Active := false;
      try
        exFileName := ExtractFilePath(Application.ExeName)+'data\'+FileName+'.gst';
        if not FileExists(exFileName) then
          exit;
        TheCDST.LoadFromFile(exFileName);
        TheCDST.Active := true;
        while not TheCDST.Eof do
        begin
          if TheCDST.FieldValues['GSMMode'] =0 then
          begin
            AMessageSendItem := TGSMMessageItem.Create;
            AMessageSendItem.MobileNo := TheCDST.FieldValues['MobileNo'];
            AMessageSendItem.SMSMessage := TheCDST.FieldValues['MSG'];
            TheSendItems.AddFromSendItem(AMessageSendItem);
          end
          else
          begin
            ADialSendItem := TGSMDialItem.Create;
            ADialSendItem.MobileNo := TheCDST.FieldValues['MobileNo'];
            ADialSendItem.isRepeat := TheCDST.FieldValues['isRepeat'];
            ADialSendItem.RepeatCount := TheCDST.FieldValues['RepeatCount'];
            ADialSendItem.DialTime := TheCDST.FieldValues['DialTime'];
            TheSendItems.AddFromSendItem(ADialSendItem);
          end;
          TheCDST.Next;
        end;
      except
      end;
    end;
  end;


var
  TCount: Integer;
  ATask: TGSMTask;
  i: Integer;
  TaskSection: String;
  CDST: TClientDataSet;
  updateType: Integer;
  str: String;
begin
  TCount := IniFile.ReadInteger('Setting','TaskCount',0);
  CDST := TClientDataSet.Create(nil);
  for i:=0 to TCount-1 do
  begin
    ATask := AddTask;
    TaskSection := 'Task'+IntToStr(i);
    ATask.TaskName := IniFile.ReadString(TaskSection,'TaskName',TaskSection);
    ATask.TaskTime := IniFile.ReadDateTime(TaskSection,'TaskTime',now);
    ATask.TaskStartTime := IniFile.ReadDateTime(TaskSection,'TaskStartTime',now);
    ATask.SMSMessage := IniFile.ReadString(TaskSection,'Message','');
    ATask.DynamicDataBase := IniFile.ReadBool(TaskSection,'DynamicDataBase',false);
    ATask.DataBaseInfo.UpdateCount := IniFile.ReadInteger(TaskSection,'DynamicCount',100);
    UpdateType := IniFile.ReadInteger(TaskSection,'DynamicType',0);
    case updateType of
      0:
        ATask.DataBaseInfo.UpdateType := utUpdate;
      1:
        ATask.DataBaseInfo.UpdateType := utDelete;
      else
        ATask.DataBaseInfo.UpdateType := utUpdate;
    end;

    ATask.FSendedCount := IniFile.ReadInteger(TaskSection,'SendedCount',0);

    ATask.DataBaseInfo.ConnectionString := IniFile.ReadString(TaskSection,'ConnectionString','');
    ATask.DataBaseInfo.TableName := IniFile.ReadString(TaskSection,'TableName','');
    ATask.DataBaseInfo.FieldName := IniFile.ReadString(TaskSection,'FieldName','');
    ATask.DataBaseInfo.KeyFieldName := IniFile.ReadString(TaskSection,'KeyFieldName','');
    ATask.DataBaseInfo.StateFieldName := IniFile.ReadString(TaskSection,'StateFieldName','');
    ATask.DataBaseInfo.MSGFieldName := IniFile.ReadString(TaskSection,'MSGFieldName','');
    str := IniFile.ReadString(TaskSection,'WaitCDSTName','');
    if str<>'' then
    begin
      ATask.WaitCDSTName := str;
      try
        LoadItemsFromCDST(CDST,ATask.WaitSendItems,str,ATask);
      except
      end;

    end;
    str := IniFile.ReadString(TaskSection,'SendCDSTName','');
    if str <>'' then
      ATask.SendCDSTName := str;
    ATask.WaitSendItems.MSG := ATask.SMSMessage;
  end;
  CDST.Free;

end;

procedure TGSMTasks.DeleteTask(Index: Integer);
var
  ATask: TGSMTask;
  DelFileName: String;
begin
  ATask := self.Task[Index];
  if ATask<>nil then
  begin
    DelFileName := ExtractFilePath(Application.ExeName)+'Data\'+ATask.WaitCDSTName+'.gst';
    if FileExists(DelFileName) then
       DeleteFile(DelFileName);
    DelFileName := ExtractFilePath(Application.ExeName)+'Data\'+ATask.SendCDSTName+'.gst';
    if FileExists(DelFileName) then
       DeleteFile(DelFileName);
    Delete(Index);
    ATask.Free;
  end;
end;

destructor TGSMTasks.Destroy;
var
  ATask: TGSMTask;
  i: Integer;
begin
  for i :=Count-1  Downto 0 do
  begin
    ATask := Task[i];
    Delete(i);
    ATask.Free;
  end;
  inherited;
end;

function TGSMTasks.CreateListCDST:  TClientDataSet;
var
  AField: TField;
begin
  Result := TClientDataSet.Create(nil);

  AField := TStringField.Create(Result);
  AField.FieldName := 'MobileNo';
  AField.Size :=255;
  AField.DataSet := Result;

  AField := TStringField.Create(Result);
  AField.FieldName := 'MSG';
  AField.Size :=1000;
  AField.DataSet := Result;

  AField := TIntegerField.Create(Result);
  AField.FieldName := 'GSMMode';
  AField.DataSet := Result;

  AField := TBooleanField.Create(Result);
  AField.FieldName := 'isRepeat';
  AField.DataSet := Result;

  AField := TIntegerField.Create(Result);
  AField.FieldName := 'RepeatCount';
  AField.DataSet := Result;

  AField := TIntegerField.Create(Result);
  AField.FieldName := 'DialTime';
  AField.DataSet := Result;
  Result.CreateDataSet;
end;

{ TGSMServer }

constructor TGSMServer.Create;
begin
  FGSMSets := TGSMSets.Create;
end;

destructor TGSMServer.Destroy;
begin
  FGSMSets.Free;
  Inherited Destroy;
end;

{ TGSMSet }

constructor TGSMSet.Create(AGSMSets:TGSMSets);
begin
  FGSMSets := AGSMSets;
  FInThread := false;
  FPort := -1;
  Fstate := gsUnknown;
  FSendCount := 0;
  FFailCount := 0;
  Fee := -1;
  FLastCheckFeeDate := Yesterday;
  FeeState := gfUnkown;
  FeeLowLevel := -1;
  FGSMCom := nil;
end;

procedure TGSMSet.DoActive;
begin
  if not FKeepIT then
  begin
    FGSMCom := TGSMCom.Create(self,FPort,true);
    FGSMCom.Resume;
    FInThread := true;
  end;
end;


procedure TGSMSet.DoFee(SendAdress,SendMessage,ReceiveAdress,FeeTemplate: String);
var
  NeedRestart: Boolean;
  Tick: Cardinal;
begin
  NeedRestart := FInThread;
  DoTerminate;
  FInThread := true;
  FGSMCom := TGSMFeeCom.Create(self,FPort,true);
  (FGSMCom as TGSMFeeCom).SetFeeInfo(SendAdress,SendMessage,ReceiveAdress,FeeTemplate);
  FGSMCom.Resume;
end;

procedure TGSMSet.DoPause;
begin
  if FInThread and  (FGSMCom<>nil) then
    FGSMCom.Pause := true;
end;

procedure TGSMSet.DoResume;
begin
  if FInThread and  (FGSMCom<>nil) then
    FGSMCom.Pause := false;
end;

procedure TGSMSet.DoTerminate;
var
  Tick: Cardinal;
  isTimeOut: Boolean;
  TimeOut: Integer;
  threadState: DWord;
begin
  if FInThread then
  begin
    FGSMCom.Terminate;
    Tick := GetTickCount;
    TimeOut := 60000;
    isTimeOut := false;
    repeat
      if GetTickCount-Tick>TimeOut then
      begin
        isTimeOut := true;
        break;
      end;
      SimpleWait(500);
    until not FInThread;

    if isTimeOut then
    try
      GetExitCodeThread(FGSMCom.Handle,threadState);
      if threadState = STILL_ACTIVE then
      begin
        FGSMCom.FreeGSMPhone;
        TerminateThread(FGSMCom.Handle,0);
      end;
    except
      try
        TerminateThread(FGSMCom.Handle,0);
      except
      end;
    end;
    FInThread := false;
    FGSMCom := nil;
  end;
end;

function TGSMSet.getGSMCom: TGSMCom;
begin
  Result := nil;
  if InThread then
    Result := FGSMCom;
end;

function TGSMSet.getIndex: Integer;
begin
  Result := FGSMSets.IndexOf(Self);
end;

procedure TGSMSet.ReActive;
begin
  DoTerminate;
  DoActive;
end;

procedure TGSMSet.SetFailCount(const Value: Integer);
begin
  FFailCount := Value;
end;

procedure TGSMSet.SetFee(const Value: Currency);
begin
  FFee := Value;
  if FFee=-1 then
    FFeeState := gfUnkown
  else if (FFeeLowLevel>=0) and (FFee<=FFeeLowLevel) then
    FFeeState := gfTooLow
  else
    FFeeState := gfNormal;
end;

procedure TGSMSet.SetFeeLowLevel(const Value: Currency);
begin
  FFeeLowLevel := Value;
end;

procedure TGSMSet.SetFeeState(const Value: TGSMFeeState);
begin
  FFeeState := Value;
end;

procedure TGSMSet.SetInThread(const Value: Boolean);
begin
  FInThread := Value;
end;

procedure TGSMSet.SetKeepIT(const Value: Boolean);
begin
  FKeepIT := Value;
  if FKeepIT and FInThread then
    DoTerminate;
end;

procedure TGSMSet.SetLastCheckFeeDate(const Value: TDate);
begin
  FLastCheckFeeDate := Value;
end;

procedure TGSMSet.SetPort(const Value: Integer);
begin
  FPort := Value;
end;

procedure TGSMSet.SetSendCount(const Value: Integer);
begin
  FSendCount := Value;
end;

procedure TGSMSet.Setstate(const Value: TGSMSetState);
begin
  Fstate := Value;
end;

procedure TGSMSet.SetTask(const Value: TGSMTask);
begin
  FTask := Value;
end;

{ TGSMSets }

function TGSMSets.FindGSMSetByPortNo(PortNo: Integer): TGSMSet;
var
  i: Integer;
begin
  Result := nil;
  for i:=0 to Count-1 do
    if GSMSet[i].Port = PortNo then
    begin
      Result := GSMSet[i];
      break;
    end;
end;

procedure TGSMSets.ForceSetPort(Str: String);
begin
  FPortString := str;
  InterParserPort;
end;

function TGSMSets.GetGSMSet(Index: Integer): TGSMSet;
begin
  Result := TGSMSet(Items[index]);
end;

function TGSMSets.GetInThreadCount: Integer;
var
  i: integer;
begin
  Result := 0;
  for i:= 0 to Count-1 do
    if GSMSet[i].InThread then
      Inc(Result);
end;

procedure TGSMSets.InterParserPort;
var
  sl: TStringlist;
  i: Integer;
  AGSMSet: TGSMSet;
begin

  sl := TStringList.Create;
  try
    ParsePortListFromStr(FPortString,sl);
    Clear;
    for i:=0 to sl.Count-1 do
    begin
      AGSMSet := TGSMSet.Create(self);
      AGSMSet.state := gsUnknown;
      AGSMSet.Port := StrToInt(sl[i]);
      Add(AGSMSet);
    end;
  except
  end;
  sl.Free;

end;

procedure TGSMSets.SetPortString(const Value: String);
begin
  FPortString := Value;
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -