-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfhem2mqtt.lpr
128 lines (108 loc) · 2.83 KB
/
fhem2mqtt.lpr
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
program fhem2mqtt;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes, SysUtils, CustApp, ufhem, laz_synapse, LAZ_MQTT, mqtt
{ you can add units after this };
type
{ TFHEM2MQTT }
TFHEM2MQTT = class(TCustomApplication)
procedure FHEMLogInfo(aInfo: string);
private
MQTTClient: TMQTTClient;
FHEMLog: TFHEMLogThread;
FName : string;
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
end;
{ TFHEM2MQTT }
procedure TFHEM2MQTT.FHEMLogInfo(aInfo: string);
var
Dev: String;
Typ: String;
reading: String;
value: String;
begin
write(StringReplace(aInfo,'<br>','',[rfReplaceAll]));
aInfo := copy(aInfo,pos(' ',aInfo)+1,length(aInfo));//Date
aInfo := copy(aInfo,pos(' ',aInfo)+1,length(aInfo));//Time
Typ := copy(aInfo,0,pos(' ',aInfo)-1);
aInfo := copy(aInfo,pos(' ',aInfo)+1,length(aInfo));
Dev := copy(aInfo,0,pos(' ',aInfo)-1);
aInfo := copy(aInfo,pos(' ',aInfo)+1,length(aInfo));
reading := copy(aInfo,0,pos(' ',aInfo)-1);
if pos(':',reading)=0 then exit;
aInfo := copy(aInfo,pos(' ',aInfo)+1,length(aInfo));
value := copy(aInfo,0,pos('<',aInfo)-1);
FName := GetOptionValue('n','topic');
if FName='' then
begin
FName := FHEMLog.Log.Sock.GetRemoteSinIP;
FName := FHEMLog.Log.Sock.ResolveIPToName(FName);
end;
if not MQTTClient.isConnected then
MQTTClient.Connect;
try
if not MQTTClient.Publish('/'+FName+'/'+Dev+'/'+copy(reading,0,pos(':',reading)-1,value) then
begin
writeln('-->failed');
raise Exception.Create('publishing failed');
end
else writeln('-->ok');
except
MQTTClient.Disconnect;
MQTTClient.Free;
MQTTClient := TMQTTClient.Create(GetOptionValue('m','mqtt'),1883);
end;
end;
procedure TFHEM2MQTT.DoRun;
var
ErrorMsg: String;
begin
// parse parameters
if HasOption('h', 'help') then begin
WriteHelp;
Terminate;
Exit;
end;
FHEMLog := TFHEMLogThread.Create(GetOptionValue('f','fhem'),True);
FhemLog.OnInfo:=@FHEMLogInfo;
MQTTClient := TMQTTClient.Create(GetOptionValue('m','mqtt'),1883);
if MQTTClient.Connect then
FHEMLog.Execute;
// stop program loop
MQTTClient.Free;
FHEMLog.Free;
Terminate;
end;
constructor TFHEM2MQTT.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException:=True;
end;
destructor TFHEM2MQTT.Destroy;
begin
inherited Destroy;
end;
procedure TFHEM2MQTT.WriteHelp;
begin
{ add your help code here }
writeln('Usage: ', ExeName, ' -hfm');
writeln('-f FHEM Instance');
writeln('-m MQTT Instance');
writeln('-n Topic (optional)');
end;
var
Application: TFHEM2MQTT;
begin
Application:=TFHEM2MQTT.Create(nil);
Application.Title:='fhm2mqtt';
Application.Run;
Application.Free;
end.