-
Notifications
You must be signed in to change notification settings - Fork 3
/
u_IBInfoDataWriter.pas
210 lines (184 loc) · 6.69 KB
/
u_IBInfoDataWriter.pas
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
unit u_IBInfoDataWriter;
interface
uses
u_IB_DBWriter, IBDatabase, IBSQL, uInfoData, uStateRec, SysUtils;
type
TIBInfoDataWriter = class(TIB_DBWriter)
private
InfoData: PInfoData;
sr: TStateRec;
qrWrStateNoGPS: TIBSQL;
qrWrStateFull: TIBSQL;
function WriteState: Boolean;
function WritePosJournal: Boolean;
function WriteStateJournal: Boolean;
public
function WriteData(AData: PByte): Boolean;
constructor Create(DB: TIBDatabase);
destructor Destroy; override;
end;
implementation
uses
u_SQLs, uJournalSpec, Variants, LogFileUnit, uCommProtocol_3C, u_ByteOrders;
{ TIBInfoDataWriter }
constructor TIBInfoDataWriter.Create(DB: TIBDatabase);
begin
inherited;
qrWrStateNoGPS := TIBSQL.Create(nil);
qrWrStateNoGPS.Database := DB;
qrWrStateNoGPS.Transaction := trWrite;
qrWrStateNoGPS.SQL.Text := SQL_STATE_NOGPS_INSERTUPDATE;
qrWrStateFull := TIBSQL.Create(nil);
qrWrStateFull.Database := DB;
qrWrStateFull.Transaction := trWrite;
qrWrStateFull.SQL.Text := SQL_STATE_INSERTUPDATE;
end;
destructor TIBInfoDataWriter.Destroy;
begin
qrWrStateNoGPS.Free;
qrWrStateFull.Free;
inherited;
end;
function TIBInfoDataWriter.WriteData(AData: PByte): Boolean;
begin
StartWork;
InfoData := PInfoData(AData);
Result := WriteState;
if Result then Result := WriteStateJournal;
if Result and (sr.latitude <> -1) and (sr.longitude <> -1) then
Result := WritePosJournal;
if Result then
CommitWork
else
RollbackWork;
end;
function TIBInfoDataWriter.WritePosJournal: Boolean;
begin
try
qrWrite.Close;
qrWrite.SQL.Text := SQL_JOURNAL_INSERT;
qrWrite.ParamByName('id_vehicle').AsInteger := sr.id_vehicle;
qrWrite.ParamByName('phone_number').AsString := FVID.Phone;
qrWrite.ParamByName('recordtype_id').AsInteger := GPS_POSITION_JOURN;
qrWrite.ParamByName('absolute_time').AsDateTime := sr.time_state;
qrWrite.ParamByName('latitude').AsFloat := sr.latitude_ex;
qrWrite.ParamByName('longitude').AsFloat := sr.longitude_ex;
qrWrite.ParamByName('altitude').AsFloat := sr.altitude_ex;
qrWrite.ParamByName('din').AsVariant := null;
qrWrite.ParamByName('dout').AsVariant := null;
qrWrite.ParamByName('adc1').AsVariant := null;
qrWrite.ParamByName('adc2').AsVariant := null;
qrWrite.ParamByName('dac').AsVariant := null;
qrWrite.ParamByName('charge_level').AsVariant := null;
qrWrite.ParamByName('gsm_signal_strength').AsVariant := null;
qrWrite.ParamByName('flags_state').AsVariant := null;
qrWrite.ParamByName('speed').AsFloat := sr.speed_ex;
qrWrite.ParamByName('course').AsFloat := sr.course_ex;
qrWrite.ParamByName('exstatus').AsVariant := null;
qrWrite.ExecQuery;
Result := True;
except
on E: Exception do begin
LogError('WritePosJournal', E.Message);
Result := False;
end;
end;
end;
function TIBInfoDataWriter.WriteState: Boolean;
var
qr: TIBSQL;
IsGPSFake: Boolean;
begin
sr.id_vehicle := FVID.ID;
sr.time_state := DecodeInfoDateTime(InfoData.UTC);
sr.latitude_ex := SwapLongWord(InfoData.Latitude);
sr.longitude_ex := SwapLongWord(InfoData.Longitude);
sr.altitude_ex := SwapLongWord(InfoData.Altitude);
sr.latitude := DecodeLatitude(sr.latitude_ex);
sr.longitude := DecodeLongitude(sr.longitude_ex);
sr.altitude := DecodeAltitude(sr.altitude_ex);
sr.din := InfoData.DIn;
sr.dout := InfoData.DOut;
sr.adc1 := InfoData.ADC1;
sr.adc2 := InfoData.ADC2;
sr.dac := InfoData.DAC;
sr.charge_level := InfoData.CL;
sr.gsm_signal_str := InfoData.SS;
sr.flags_state := InfoData.Status;
sr.speed_ex := Swap(InfoData.Speed);
sr.course_ex := Swap(InfoData.Course);
sr.speed := DecodeSpeed(sr.speed_ex);
sr.course := DecodeCourse(sr.course_ex);
sr.running_hours := SwapLongWord(InfoData.RunningHours);
sr.journal_size := SwapLongWord(InfoData.JournalSize);
sr.newjourn_size := SwapLongWord(InfoData.NewJournalSize);
//sr.exstatus := Swap(InfoData.exstatus);
sr.exstatus := 0;
IsGPSFake := (sr.latitude = GPS_FAKE) or (sr.longitude = GPS_FAKE);
if IsGPSFake then
qr := qrWrStateNoGPS
else
qr := qrWrStateFull;
qr.ParamByName('id_vehicle').AsInteger := sr.id_vehicle;
qr.ParamByName('time_state').AsDateTime := sr.time_state;
if not IsGPSFake then
begin
qr.ParamByName('latitude').AsFloat := sr.latitude;
qr.ParamByName('longitude').AsFloat := sr.longitude;
qr.ParamByName('altitude').AsFloat := sr.altitude;
end;
qr.ParamByName('din').AsShort := sr.din;
qr.ParamByName('dout').AsShort := sr.dout;
qr.ParamByName('adc1').AsShort := sr.adc1;
qr.ParamByName('adc2').AsShort := sr.adc2;
qr.ParamByName('dac').AsShort := sr.dac;
qr.ParamByName('charge_level').AsShort := sr.charge_level;
qr.ParamByName('gsm_signal_strength').AsShort := sr.gsm_signal_str;
qr.ParamByName('flags_state').AsShort := sr.flags_state;
if not IsGPSFake then
begin
qr.ParamByName('speed').AsFloat := sr.speed;
qr.ParamByName('course').AsFloat := sr.course;
end;
qr.ParamByName('running_hours').AsFloat := sr.running_hours;
qr.ParamByName('journal_size').AsFloat := sr.journal_size;
qr.ParamByName('newjournal_size').AsFloat := sr.newjourn_size;
qr.ParamByName('exstatus').AsInteger := sr.exstatus;
//OutputDebugString(PChar(IntToStr(sr.exstatus)));
qr.ExecQuery;
Result := True;
end;
function TIBInfoDataWriter.WriteStateJournal: Boolean;
begin
try
qrWrite.Close;
qrWrite.SQL.Text := SQL_JOURNAL_INSERT;
qrWrite.ParamByName('id_vehicle').AsInteger := sr.id_vehicle;
qrWrite.ParamByName('phone_number').AsString := FVID.Phone;
qrWrite.ParamByName('recordtype_id').AsInteger := STATE_JOURN;
qrWrite.ParamByName('absolute_time').AsDateTime := sr.time_state;
qrWrite.ParamByName('latitude').AsVariant := null;
qrWrite.ParamByName('longitude').AsVariant := null;
qrWrite.ParamByName('altitude').AsVariant := null;
qrWrite.ParamByName('din').AsShort := sr.din;
qrWrite.ParamByName('dout').AsShort := sr.dout;
qrWrite.ParamByName('adc1').AsShort := sr.adc1;
qrWrite.ParamByName('adc2').AsShort := sr.adc2;
qrWrite.ParamByName('dac').AsShort := sr.dac;
qrWrite.ParamByName('charge_level').AsShort := sr.charge_level;
qrWrite.ParamByName('gsm_signal_strength').AsShort := sr.gsm_signal_str;
qrWrite.ParamByName('flags_state').AsShort := sr.flags_state;
qrWrite.ParamByName('speed').AsVariant := null;
qrWrite.ParamByName('course').AsVariant := null;
qrWrite.ParamByName('exstatus').AsInteger := sr.exstatus;
qrWrite.ExecQuery;
Result := True;
except
on E: Exception do
begin
LogError('WriteStateJournal', E.Message);
Result := False;
end;
end;
end;
end.