-
Notifications
You must be signed in to change notification settings - Fork 26
/
goveebttemplogger.cpp
5315 lines (5250 loc) · 258 KB
/
goveebttemplogger.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2024 William C Bonner
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// GoveeBTTempLogger is designed as a project to run on a Raspberry Pi with
// Bluetooth Low Energy support. It listens for advertisments from Govee
// https://www.govee.com/product/thermometers-hygrometers/indoor-thermometers-hygrometers
// Currently the H5074, GVH5075, and GVH5177 are decoded and logged.
// Each unit has its data logged to it's own file, with a new file created daily.
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// Links I've found useful in learning about Bluetooth Low Energy (BLE) or Govee
// https://github.com/Thrilleratplay/GoveeWatcher
// https://github.com/neilsheps/GoveeTemperatureAndHumidity
// https://github.com/carsonmcdonald/bluez-experiments/blob/master/experiments/scantest.c
// https://people.csail.mit.edu/albert/bluez-intro/index.html
// https://reelyactive.github.io/ble-identifier-reference.html
// https://ukbaz.github.io/howto/beacon_scan_cmd_line.html
// https://github.com/ukBaz/ukBaz.github.io/blob/master/howto/beacon_scan_cmd_line.html
// http://kktechkaizen.blogspot.com/2014/10/bluetooth-technology-overview.html
// https://github.com/microsoftarchive/msdn-code-gallery-microsoft/tree/master/Official%20Windows%20Platform%20Sample/Bluetooth%20LE%20Explorer%20sample
// https://docs.microsoft.com/en-us/windows/win32/bluetooth/bluetooth-programming-with-windows-sockets
// https://www.reddit.com/r/Govee/comments/f1dfcd/home_assistant_component_for_h5074_and_h5075/fi7hnic/
// https://unix.stackexchange.com/questions/96106/bluetooth-le-scan-as-non-root
// https://docs.microsoft.com/en-us/cpp/linux/configure-a-linux-project?view=vs-2017
// https://reelyactive.github.io/diy/best-practices-ble-identifiers/
// https://github.com/pauloborges/bluez/blob/master/doc/mgmt-api.txt
// https://gist.github.com/mironovdm/cb7f47e8d898e9a3977fc888d990e8a9
// https://www.argenox.com/library/bluetooth-low-energy/using-raspberry-pi-ble/
//
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <dbus/dbus.h> // sudo apt install libdbus-1-dev
#include <filesystem>
#include <fstream>
#include <getopt.h>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <locale>
#include <map>
#include <netdb.h>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> // For close()
#include <utime.h>
#include <vector>
#ifdef _BLUEZ_HCI_
#include <arpa/inet.h>
#include <bluetooth/bluetooth.h> // apt install libbluetooth-dev
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include "att-types.h"
#include "uuid.h"
#endif // _BLUEZ_HCI_
#ifndef __BLUETOOTH_H
/* BD Address */
typedef struct {
uint8_t b[6];
} __attribute__((packed)) bdaddr_t;
#endif // !bdaddr_t
#include "wimiso8601.h"
/////////////////////////////////////////////////////////////////////////////
#if __has_include("goveebttemplogger-version.h")
#include "goveebttemplogger-version.h"
#endif
#ifndef GoveeBTTempLogger_VERSION
#define GoveeBTTempLogger_VERSION "(non-CMake)"
#endif // !GoveeBTTempLogger_VERSION
/////////////////////////////////////////////////////////////////////////////
static const std::string ProgramVersionString("GoveeBTTempLogger Version " GoveeBTTempLogger_VERSION " Built on: " __DATE__ " at " __TIME__);
/////////////////////////////////////////////////////////////////////////////
#ifdef _BLUEZ_HCI_
#ifndef BT_HCI_CMD_LE_SET_EXT_SCAN_PARAMS
#define BT_HCI_CMD_LE_SET_EXT_SCAN_PARAMS 0x2041
int hci_le_set_ext_scan_parameters(int dd, uint8_t type, uint16_t interval, uint16_t window, uint8_t own_type, uint8_t filter, int to)
{
struct bt_hci_cmd_le_set_ext_scan_params {
uint8_t own_addr_type;
uint8_t filter_policy;
uint8_t num_phys;
uint8_t type;
uint16_t interval;
uint16_t window;
} __attribute__((packed)) param_cp;
memset(¶m_cp, 0, sizeof(param_cp));
param_cp.type = type;
param_cp.interval = interval;
param_cp.window = window;
param_cp.own_addr_type = own_type;
param_cp.filter_policy = filter;
param_cp.num_phys = 1;
uint8_t status;
struct hci_request rq;
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_LE_CTL;
rq.ocf = BT_HCI_CMD_LE_SET_EXT_SCAN_PARAMS;
rq.cparam = ¶m_cp;
rq.clen = sizeof(bt_hci_cmd_le_set_ext_scan_params);
rq.rparam = &status;
rq.rlen = 1;
if (hci_send_req(dd, &rq, to) < 0)
return -1;
if (status) {
errno = EIO;
return -1;
}
return 0;
}
#endif // !BT_HCI_CMD_LE_SET_EXT_SCAN_PARAMS
#ifndef BT_HCI_CMD_LE_SET_EXT_SCAN_ENABLE
#define BT_HCI_CMD_LE_SET_EXT_SCAN_ENABLE 0x2042
int hci_le_set_ext_scan_enable(int dd, uint8_t enable, uint8_t filter_dup, int to)
{
struct bt_hci_cmd_le_set_ext_scan_enable {
uint8_t enable;
uint8_t filter_dup;
uint16_t duration;
uint16_t period;
} __attribute__((packed)) scan_cp;
memset(&scan_cp, 0, sizeof(scan_cp));
scan_cp.enable = enable;
scan_cp.filter_dup = filter_dup;
uint8_t status;
struct hci_request rq;
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_LE_CTL;
rq.ocf = BT_HCI_CMD_LE_SET_EXT_SCAN_ENABLE;
rq.cparam = &scan_cp;
rq.clen = sizeof(scan_cp);
rq.rparam = &status;
rq.rlen = 1;
if (hci_send_req(dd, &rq, to) < 0)
return -1;
if (status) {
errno = EIO;
return -1;
}
return 0;
}
#endif // !BT_HCI_CMD_LE_SET_EXT_SCAN_ENABLE
#ifndef BT_HCI_CMD_LE_SET_RANDOM_ADDRESS
// 2023-11-29 Added this function to fix problem with Raspberry Pi Zero 2 W Issue https://github.com/wcbonner/GoveeBTTempLogger/issues/50
int hci_le_set_random_address(int dd, int to)
{
le_set_random_address_cp scan_cp{ 0 };
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count()); // 2023-12-01 switch to c++ std library <random>
for (auto& b : scan_cp.bdaddr.b)
b = generator() % 256;
uint8_t status;
struct hci_request rq;
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_LE_CTL;
rq.ocf = OCF_LE_SET_RANDOM_ADDRESS;
rq.cparam = &scan_cp;
rq.clen = sizeof(scan_cp);
rq.rparam = &status;
rq.rlen = 1;
if (hci_send_req(dd, &rq, to) < 0)
return -1;
if (status) {
errno = EIO;
return -1;
}
return 0;
}
#endif // BT_HCI_CMD_LE_SET_RANDOM_ADDRESS
#endif // _BLUEZ_HCI_
/////////////////////////////////////////////////////////////////////////////
int ConsoleVerbosity(1);
bool UseBluetooth(true);
std::filesystem::path LogDirectory; // If this remains empty, log Files are not created.
std::filesystem::path CacheDirectory; // If this remains empty, cache Files are not used. Cache Files should greatly speed up startup of the program if logged data runs multiple years over many devices.
std::filesystem::path SVGDirectory; // If this remains empty, SVG Files are not created. If it's specified, _day, _week, _month, and _year.svg files are created for each bluetooth address seen.
int SVGBattery(0); // 0x01 = Draw Battery line on daily, 0x02 = Draw Battery line on weekly, 0x04 = Draw Battery line on monthly, 0x08 = Draw Battery line on yearly
int SVGMinMax(0); // 0x01 = Draw Temperature and Humiditiy Minimum and Maximum line on daily, 0x02 = on weekly, 0x04 = on monthly, 0x08 = on yearly
bool SVGFahrenheit(true);
std::filesystem::path SVGTitleMapFilename;
std::filesystem::path SVGIndexFilename;
int LogFileTime(60);
int MinutesAverage(5);
int DaysBetweenDataDownload(0);
// The following details were taken from https://github.com/oetiker/mrtg
const size_t DAY_COUNT(600); /* 400 samples is 33.33 hours */
const size_t WEEK_COUNT(600); /* 400 samples is 8.33 days */
const size_t MONTH_COUNT(600); /* 400 samples is 33.33 days */
const size_t YEAR_COUNT(2 * 366); /* 1 sample / day, 366 days, 2 years */
const size_t DAY_SAMPLE(5 * 60); /* Sample every 5 minutes */
const size_t WEEK_SAMPLE(30 * 60); /* Sample every 30 minutes */
const size_t MONTH_SAMPLE(2 * 60 * 60); /* Sample every 2 hours */
const size_t YEAR_SAMPLE(24 * 60 * 60); /* Sample every 24 hours */
/////////////////////////////////////////////////////////////////////////////
// Class I'm using for storing raw data from the Govee thermometers
enum class ThermometerType
{
Unknown = 0,
H5072 = 5072,
H5074 = 5074,
H5075 = 5075,
H5100 = 5100,
H5101 = 5101,
H5104 = 5104,
H5105 = 5105,
H5174 = 5174,
H5177 = 5177,
H5179 = 5179,
H5181 = 5181,
H5182 = 5182,
H5183 = 5183,
H5184 = 5184,
H5055 = 5055,
};
std::string ThermometerType2String(const ThermometerType GoveeModel)
{
switch (GoveeModel)
{
case ThermometerType::H5072:
return(std::string("(GVH5072)"));
case ThermometerType::H5074:
return(std::string("(GVH5074)"));
case ThermometerType::H5075:
return(std::string("(GVH5075)"));
case ThermometerType::H5100:
return(std::string("(GVH5100)"));
case ThermometerType::H5101:
return(std::string("(GVH5101)"));
case ThermometerType::H5104:
return(std::string("(GVH5104)"));
case ThermometerType::H5105:
return(std::string("(GVH5105)"));
case ThermometerType::H5174:
return(std::string("(GVH5174)"));
case ThermometerType::H5177:
return(std::string("(GVH5177)"));
case ThermometerType::H5179:
return(std::string("(GVH5179)"));
case ThermometerType::H5181:
return(std::string("(GVH5181)"));
case ThermometerType::H5182:
return(std::string("(GVH5182)"));
case ThermometerType::H5183:
return(std::string("(GVH5183)"));
case ThermometerType::H5184:
return(std::string("(GVH5184)"));
case ThermometerType::H5055:
return(std::string("(GVH5055)"));
}
return(std::string("(ThermometerType::Unknown)"));
}
ThermometerType String2ThermometerType(const std::string Text)
{
ThermometerType rval = ThermometerType::Unknown;
// https://regex101.com/ and https://en.cppreference.com/w/cpp/regex/regex_search
if (std::regex_search(Text, std::regex("GVH5100")))
rval = ThermometerType::H5100;
else if (std::regex_search(Text, std::regex("GVH5101")))
rval = ThermometerType::H5101;
else if (std::regex_search(Text, std::regex("GVH5104")))
rval = ThermometerType::H5104;
else if (std::regex_search(Text, std::regex("GVH5105")))
rval = ThermometerType::H5105;
else if (std::regex_search(Text, std::regex("GVH5174")))
rval = ThermometerType::H5174;
else if (std::regex_search(Text, std::regex("GVH5177")))
rval = ThermometerType::H5177;
else if (std::regex_search(Text, std::regex("GVH5072")))
rval = ThermometerType::H5072;
else if (std::regex_search(Text, std::regex("GVH5075")))
rval = ThermometerType::H5075;
else if (std::regex_search(Text, std::regex("Govee_H5074|GVH5074")))
rval = ThermometerType::H5074;
else if (std::regex_search(Text, std::regex("Govee_H5179|GVH5179")))
rval = ThermometerType::H5179;
//The Bluetooth SIG maintains a list of "Assigned Numbers" that includes those UUIDs found in the sample app: https://www.bluetooth.com/specifications/assigned-numbers/
//Although UUIDs are 128 bits in length, the assigned numbers for Bluetooth LE are listed as 16 bit hex values because the lower 96 bits are consistent across a class of attributes.
//For example, all BLE characteristic UUIDs are of the form:
//0000XXXX-0000-1000-8000-00805f9b34fb
else if (std::regex_search(Text, std::regex("GVH5181|00008151-0000-1000-8000-00805f9b34fb")))
rval = ThermometerType::H5181;
else if (std::regex_search(Text, std::regex("GVH5182|00008251-0000-1000-8000-00805f9b34fb")))
rval = ThermometerType::H5182;
//[2024-08-15T16:07:11] [C3:31:30:30:13:27] UUIDs: 00008251-0000-1000-8000-00805f9b34fb
//[2024-08-15T16:07:11] [C3:31:30:30:13:27] ManufacturerData: *** Meat Thermometer *** 1330:2701000101e4018008341cdc8008341cdc
//[2024-08-15T16:07:11] [C3:31:30:30:13:27] (Temp) 21°C (Alarm) 73.88°C (Temp) 21°C (Alarm) 73.88°C (Humidity) 0% (Battery) 100% (GVH5182)
else if (std::regex_search(Text, std::regex("GVH5183|00008351-0000-1000-8000-00805f9b34fb")))
rval = ThermometerType::H5183;
//[2024-08-15T15:58:15] [A4:C1:38:5D:A1:B4] UUIDs: 00008351-0000-1000-8000-00805f9b34fb
//[2024-08-15T15:58:15] [A4:C1:38:5D:A1:B4] ManufacturerData: *** Meat Thermometer *** a15d:b401000101e4008b083426480000 'Apple, Inc.' 004c:0215494e54454c4c495f524f434b535f48575075f2ff0c
//[2024-08-15T15:58:15] [A4:C1:38:5D:A1:B4] (Temp) 21°C (Alarm) 98°C (Humidity) 0% (Battery) 100% (GVH5183)
else if (std::regex_search(Text, std::regex("GVH5184|00008451-0000-1000-8000-00805f9b34fb")))
rval = ThermometerType::H5184;
else if (std::regex_search(Text, std::regex("GVH5055|00005550-0000-1000-8000-00805f9b34fb")))
rval = ThermometerType::H5055;
return(rval);
}
class Govee_Temp {
public:
time_t Time;
std::string WriteTXT(const char seperator = '\t') const;
std::string WriteCache(void) const;
std::string WriteConsole(void) const;
bool ReadCache(const std::string& data);
bool ReadMSG(const uint16_t Manufacturer, const std::vector<uint8_t>& Data);
Govee_Temp() : Time(0), Temperature{ 0, 0, 0, 0 }, TemperatureMin{ DBL_MAX, DBL_MAX, DBL_MAX, DBL_MAX }, TemperatureMax{ -DBL_MAX, -DBL_MAX, -DBL_MAX, -DBL_MAX }, Humidity(0), HumidityMin(DBL_MAX), HumidityMax(-DBL_MAX), Battery(INT_MAX), Averages(0), Model(ThermometerType::Unknown) { };
Govee_Temp(const time_t tim, const double tem, const double hum, const int bat)
{
Time = tim;
Temperature[0] = tem;
TemperatureMin[0] = tem;
TemperatureMax[0] = tem;
Humidity = hum;
HumidityMin = hum;
HumidityMax = hum;
Battery = bat;
Averages = 1;
};
Govee_Temp(const std::string& data);
double GetTemperature(const bool Fahrenheit = false, const int index = 0) const { if (Fahrenheit) return((Temperature[index] * 9.0 / 5.0) + 32.0); return(Temperature[index]); };
double GetTemperatureMin(const bool Fahrenheit = false, const int index = 0) const { if (Fahrenheit) return(std::min(((Temperature[index] * 9.0 / 5.0) + 32.0), ((TemperatureMin[index] * 9.0 / 5.0) + 32.0))); return(std::min(Temperature[index], TemperatureMin[index])); };
double GetTemperatureMax(const bool Fahrenheit = false, const int index = 0) const { if (Fahrenheit) return(std::max(((Temperature[index] * 9.0 / 5.0) + 32.0), ((TemperatureMax[index] * 9.0 / 5.0) + 32.0))); return(std::max(Temperature[index], TemperatureMax[index])); };
void SetMinMax(const Govee_Temp& a);
double GetHumidity(void) const { return(Humidity); };
double GetHumidityMin(void) const { return(std::min(Humidity, HumidityMin)); };
double GetHumidityMax(void) const { return(std::max(Humidity, HumidityMax)); };
int GetBattery(void) const { return(Battery); };
ThermometerType GetModel(void) const { return(Model); };
const std::string GetModelAsString(void) const { return(ThermometerType2String(Model)); };
ThermometerType SetModel(const std::string& Name);
ThermometerType SetModel(const unsigned short* UUID);
ThermometerType SetModel(const ThermometerType newModel) { ThermometerType oldModel = Model; Model = newModel; return(oldModel); };
enum granularity { day, week, month, year };
void NormalizeTime(granularity type);
granularity GetTimeGranularity(void) const;
bool IsValid(void) const { return((Averages > 0) && (Model != ThermometerType::Unknown)); };
Govee_Temp& operator +=(const Govee_Temp& b);
protected:
double Temperature[4];
double TemperatureMin[4];
double TemperatureMax[4];
double Humidity;
double HumidityMin;
double HumidityMax;
int Battery;
int Averages;
ThermometerType Model;
};
Govee_Temp::Govee_Temp(const std::string & data) // Read data from the Log File
{
std::istringstream TheLine(data);
// erase any nulls from the data. these are occasionally in the log file when the platform crashed during a write to the logfile.
while (TheLine.peek() == '\000')
TheLine.get();
std::string theDay;
TheLine >> theDay;
std::string theHour;
TheLine >> theHour;
std::string theDate(theDay + " " + theHour);
Time = ISO8601totime(theDate);
TheLine >> Temperature[0];
TemperatureMin[0] = TemperatureMax[0] = Temperature[0];
TheLine >> Humidity;
HumidityMin = HumidityMax = Humidity;
TheLine >> Battery;
if (!TheLine.eof())
{
int theModel(0);
TheLine >> theModel;
switch (theModel)
{
case 5181:
Model = ThermometerType::H5181;
break;
case 5182:
Model = ThermometerType::H5182;
break;
case 5183:
Model = ThermometerType::H5183;
break;
case 5184:
Model = ThermometerType::H5184;
break;
case 5055:
Model = ThermometerType::H5055;
break;
default:
Model = ThermometerType::Unknown;
}
unsigned long index = 1;
while ((!TheLine.eof()) && (index < (sizeof(Temperature) / sizeof(Temperature[0]))))
{
TheLine >> Temperature[index];
TemperatureMin[index] = TemperatureMax[index] = Temperature[index];
index++;
}
}
time_t timeNow(0);
time(&timeNow);
if (Time <= timeNow) // Only validate data from the past.
Averages = 1;
// h5074, h5075, h5100, h5179 Temperature Range = -20C to 60C
// h5103 Temperature Range = 0C to 50C
if (Temperature[0] < -20)
Averages = 0; // invalidate the data
}
std::string Govee_Temp::WriteTXT(const char seperator) const
{
std::ostringstream ssValue;
ssValue << timeToExcelDate(Time);
ssValue << seperator << Temperature[0];
ssValue << seperator << Humidity;
ssValue << seperator << Battery;
if (Model == ThermometerType::H5181)
{
ssValue << seperator << 5181;
ssValue << seperator << Temperature[1];
}
if (Model == ThermometerType::H5182)
{
ssValue << seperator << 5182;
ssValue << seperator << Temperature[1];
ssValue << seperator << Temperature[2];
ssValue << seperator << Temperature[3];
}
if (Model == ThermometerType::H5183)
{
ssValue << seperator << 5183;
ssValue << seperator << Temperature[1];
}
if (Model == ThermometerType::H5184)
{
ssValue << seperator << 5184;
ssValue << seperator << Temperature[1];
ssValue << seperator << Temperature[2];
ssValue << seperator << Temperature[3];
}
if (Model == ThermometerType::H5055)
{
ssValue << seperator << 5055;
ssValue << seperator << Temperature[1];
ssValue << seperator << Temperature[2];
ssValue << seperator << Temperature[3];
}
return(ssValue.str());
}
std::string Govee_Temp::WriteCache(void) const
{
std::ostringstream ssValue;
ssValue << Time;
for (auto a : Temperature)
ssValue << "\t" << a;
for (auto a : TemperatureMin)
ssValue << "\t" << a;
for (auto a : TemperatureMax)
ssValue << "\t" << a;
ssValue << "\t" << Humidity;
ssValue << "\t" << HumidityMin;
ssValue << "\t" << HumidityMax;
ssValue << "\t" << Battery;
ssValue << "\t" << Averages;
ssValue << "\t" << GetModelAsString();
return(ssValue.str());
}
std::string Govee_Temp::WriteConsole(void) const
{
std::ostringstream ssValue;
ssValue << "(Temp) " << std::setw(4) << std::dec << std::fixed << std::setprecision(1) << GetTemperature() << "\u00B0" << "C";
if ((Model == ThermometerType::H5182) || (Model == ThermometerType::H5184) || (Model == ThermometerType::H5055))
{
ssValue << " (Alarm) " << GetTemperature(false, 1) << "\u00B0" << "C";
ssValue << " (Temp) " << GetTemperature(false, 2) << "\u00B0" << "C";
ssValue << " (Alarm) " << GetTemperature(false, 3) << "\u00B0" << "C";
}
if (Model == ThermometerType::H5183)
ssValue << " (Alarm) " << GetTemperature(false, 1) << "\u00B0" << "C";
if (!((Model == ThermometerType::H5183) || (Model == ThermometerType::H5182) || (Model == ThermometerType::H5184) || (Model == ThermometerType::H5055)))
ssValue << " (Humidity) " << std::setw(5) << std::right << GetHumidity() << std::left << "%";
ssValue << " (Battery) " << std::setw(3) << std::right << std::setprecision(0) << GetBattery() << std::left << "%";
ssValue << " " << GetModelAsString();
return(ssValue.str());
}
bool Govee_Temp::ReadCache(const std::string& data)
{
bool rval = false;
std::istringstream ssValue(data);
ssValue >> Time;
for (auto & a : Temperature)
ssValue >> a;
for (auto & a : TemperatureMin)
ssValue >> a;
for (auto & a : TemperatureMax)
ssValue >> a;
ssValue >> Humidity;
ssValue >> HumidityMin;
ssValue >> HumidityMax;
ssValue >> Battery;
ssValue >> Averages;
return(rval);
}
ThermometerType Govee_Temp::SetModel(const std::string& Name)
{
ThermometerType rval = Model;
Model = String2ThermometerType(Name);
return(rval);
}
ThermometerType Govee_Temp::SetModel(const unsigned short* UUID)
{
ThermometerType rval = Model;
// 88EC could be either GVH5075_ or GVH5174_
if (0x8151 == *UUID)
Model = ThermometerType::H5181;
else if (0x8251 == *UUID)
Model = ThermometerType::H5182;
else if (0x8351 == *UUID)
Model = ThermometerType::H5183;
else if (0x8451 == *UUID)
Model = ThermometerType::H5184;
else if (0x5550 == *UUID)
Model = ThermometerType::H5055;
return(rval);
}
bool Govee_Temp::ReadMSG(const uint16_t Manufacturer, const std::vector<uint8_t>& Data) // Decode data from the BlueZ DBus interface
{
bool rval = false;
if ((Manufacturer == 0xec88) && (Data.size() == 7))// Govee_H5074_xxxx
{
if (Model == ThermometerType::Unknown)
Model = ThermometerType::H5074;
//[2024-08-12T22:53:41] [E3:5E:CC:21:5C:0F] Name: Govee_H5074_5C0F
//[2024-08-12T22:53:41] [E3:5E:CC:21:5C:0F] ManufacturerData: ec88:00f8099f1c6402
//[2024-08-12T22:53:41] [E3:5E:CC:21:5C:0F] (Temp) 25.52°C (Humidity) 73.27% (Battery) 100% (GVH5074)
short iTemp = short(Data[2]) << 8 | short(Data[1]);
int iHumidity = int(Data[4]) << 8 | int(Data[3]);
Temperature[0] = float(iTemp) / 100.0;
Humidity = float(iHumidity) / 100.0;
Battery = int(Data[5]);
if ((Temperature[0] > -20) && (Temperature[0] < 60))
Averages = 1;
time(&Time);
TemperatureMin[0] = TemperatureMax[0] = Temperature[0]; //HACK: make sure that these values are set
rval = true;
}
else if ((Manufacturer == 0xec88) && (Data.size() == 6))// GVH5075_xxxx
{
if (Model == ThermometerType::Unknown)
Model = ThermometerType::H5075;
//[2024-08-12T23:09:07] [A4:C1:38:37:BC:AE] Name: GVH5075_BCAE
//[2024-08-12T23:09:07] [A4:C1:38:37:BC:AE] ManufacturerData: ec88:000418876100 004c:0215494e54454c4c495f524f434b535f48575075f2ffc2
//[2024-08-12T23:09:07] [A4:C1:38:37:BC:AE] (Temp) 26.8°C (Humidity) 42.3% (Battery) 97% (GVH5075)
int iTemp = int(Data[1]) << 16 | int(Data[2]) << 8 | int(Data[3]);
bool bNegative = iTemp & 0x800000; // check sign bit
iTemp = iTemp & 0x7ffff; // mask off sign bit
Temperature[0] = float(iTemp / 1000) / 10.0; // issue #49 fix.
// After converting the hexadecimal number into decimal the first three digits are the
// temperature and the last three digits are the humidity.So "03519e" converts to "217502"
// which means 21.7 °C and 50.2 % humidity without any rounding.
if (bNegative) // apply sign bit
Temperature[0] = -1.0 * Temperature[0];
Humidity = float(iTemp % 1000) / 10.0;
Battery = int(Data[4]);
if ((Temperature[0] > -20) && (Temperature[0] < 60))
Averages = 1;
time(&Time);
TemperatureMin[0] = TemperatureMax[0] = Temperature[0]; //HACK: make sure that these values are set
rval = true;
}
else if ((Manufacturer == 0x0001) && (Data.size() == 6))// GVH5177_xxxx or GVH5174_xxxx or GVH5100_xxxx
{
// This is a guess based on the H5075 3 byte encoding
// It appears that the H5174 uses the exact same data format as the H5177, with the difference being the broadcast name starting with GVH5174_
//[2024-08-13T00:09:15] [A4:C1:38:0D:3B:10] Name: GVH5177_3B10
//[2024-08-13T00:09:15] [A4:C1:38:0D:3B:10] ManufacturerData: 0001:010104245d54 004c:0215494e54454c4c495f524f434b535f48575177f2ffc2
//[2024-08-13T00:09:15] [A4:C1:38:0D:3B:10] (Temp) 27.1453°C (Humidity) 45.3% (Battery) 84% (GVH5177)
int iTemp = int(Data[2]) << 16 | int(Data[3]) << 8 | int(Data[4]);
bool bNegative = iTemp & 0x800000; // check sign bit
iTemp = iTemp & 0x7ffff; // mask off sign bit
Temperature[0] = float(iTemp) / 10000.0;
Humidity = float(iTemp % 1000) / 10.0;
if (bNegative) // apply sign bit
Temperature[0] = -1.0 * Temperature[0];
Battery = int(Data[5]);
if ((Temperature[0] > -20) && (Temperature[0] < 60))
Averages = 1;
time(&Time);
TemperatureMin[0] = TemperatureMax[0] = Temperature[0]; //HACK: make sure that these values are set
rval = true;
}
else if ((Manufacturer == 0xec88) && (Data.size() == 9)) // Govee_H5179
{
if (Model == ThermometerType::Unknown)
Model = ThermometerType::H5179;
// This is from data provided in https://github.com/wcbonner/GoveeBTTempLogger/issues/36
// 0188EC00 0101 0A0A B018 64 (Temp) 25.7°C (Humidity) 63.2% (Battery) 100% (GVH5179)
// 2 3 4 5 6 7 8 9 1011 12
short iTemp = short(Data[5]) << 8 | short(Data[4]);
int iHumidity = int(Data[7]) << 8 | int(Data[6]);
Temperature[0] = float(iTemp) / 100.0;
Humidity = float(iHumidity) / 100.0;
Battery = int(Data[8]);
if ((Temperature[0] > -20) && (Temperature[0] < 60))
Averages = 1;
time(&Time);
TemperatureMin[0] = TemperatureMax[0] = Temperature[0]; //HACK: make sure that these values are set
rval = true;
}
else if (0x004c != Manufacturer) // Ignore 'Apple, Inc.'
{
if (Data.size() == 14) // I'm not checking the Manufacturer data because it appears to be part of the Bluetooth Address on this device
{
// Govee Bluetooth Wireless Meat Thermometer, Digital Grill Thermometer with 1 Probe, 230ft Remote Temperature Monitor, Smart Kitchen Cooking Thermometer, Alert Notifications for BBQ, Oven, Smoker, Cakes
// https://www.amazon.com/gp/product/B092ZTD96V
// The probe measuring range is 0° to 300°C /32° to 572°F.
//[2024-08-14T16:43:01] [A4:C1:38:5D:A1:B4] ManufacturerData: a15d:b401000101e4008b09c426480000 004c:0215494e54454c4c495f524f434b535f48575075f2ff0c
//[2024-08-14T16:43:01] [A4:C1:38:5D:A1:B4] (Temp) 25°C (Alarm) 98°C (Humidity) 0% (Battery) 100% (GVH5183)
short iTemp = short(Data[8]) << 8 | short(Data[9]);
Temperature[0] = float(iTemp) / 100.0;
iTemp = short(Data[10]) << 8 | short(Data[11]);
Temperature[1] = float(iTemp) / 100.0; // This appears to be the alarm temperature.
Humidity = 0;
Battery = int(Data[5] & 0x7F);
Averages = 1;
time(&Time);
for (unsigned long index = 0; index < (sizeof(Temperature) / sizeof(Temperature[0])); index++)
TemperatureMin[index] = TemperatureMax[index] = Temperature[index]; //HACK: make sure that these values are set
rval = IsValid();
}
else if (Data.size() == 17) // I'm not checking the Manufacturer data because it appears to be part of the Bluetooth Address on this device
{
// Govee Bluetooth Meat Thermometer, 230ft Range Wireless Grill Thermometer Remote Monitor with Temperature Probe Digital Grilling Thermometer with Smart Alerts for Smoker , Cooking, BBQ, Kitchen, Oven
// https://www.amazon.com/gp/product/B094N2FX9P
// If the probe is not connected to the device, the temperature data is set to FFFF.
// If the alarm is not set for the probe, the data is set to FFFF.
//[2024-08-14T17:47:34] [C3:31:30:30:13:27] ManufacturerData: 1330:2701000101e4018008341cdc8008341cdc 004c:0215494e54454c4c495f524f434b535f48575075f2ff0c
//[2024-08-14T17:47:34] [C3:31:30:30:13:27] (Temp) 21°C (Alarm) 73.88°C (Temp) 21°C (Alarm) 73.88°C (Humidity) 0% (Battery) 100% (GVH5182)
// The H5184 seems to use this same data format, and alternates sending probes 1-2 and 3-4.
// I've not figured out how to recognize which set of probes are currently being sent.
// it may be a single bit in byte 12.
//wim@WimPi4:~ $ ~/GoveeBTTempLogger/build/goveebttemplogger -v 2 | grep CF\:32\:32\:36\:4F\:62
// Alarms set to 60, 71, 49, and 93 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
//[2024-08-15T03:08:39] [CF:32:32:36:4F:62] ManufacturerData: 4f36:62 01000101 64 0180 0834 1770 89 0898 1bbc
//[2024-08-15T03:08:39] [CF:32:32:36:4F:62] (Temp) 21°C (Alarm) 60°C (Temp) 22°C (Alarm) 71°C (Battery) 100% (GVH5182)
//[2024-08-15T03:08:40] [CF:32:32:36:4F:62] ManufacturerData: 4f36:62 01000101 64 028a 0834 1324 8c 0898 2454
//[2024-08-15T03:08:40] [CF:32:32:36:4F:62] (Temp) 21°C (Alarm) 49°C (Temp) 22°C (Alarm) 93°C (Battery) 100% (GVH5182)
//[2024-08-15T03:08:41] [CF:32:32:36:4F:62] ManufacturerData: 4f36:62 01000101 64 0180 0834 1770 89 0898 1bbc 004c:0215494e54454c4c495f524f434b535f48575075f2ff0c
//[2024-08-15T03:08:41] [CF:32:32:36:4F:62] (Temp) 21°C (Alarm) 60°C (Temp) 22°C (Alarm) 71°C (Battery) 100% (GVH5182)
//[2024-08-15T03:08:42] [CF:32:32:36:4F:62] ManufacturerData: 4f36:62 01000101 64 028a 0834 1324 8c 0898 2454
//[2024-08-15T03:08:42] [CF:32:32:36:4F:62] (Temp) 21°C (Alarm) 49°C (Temp) 22°C (Alarm) 93°C (Battery) 100% (GVH5182)
short iTemp = short(Data[8]) << 8 | short(Data[9]); // Probe 1 Temperature
Temperature[0] = float(iTemp) / 100.0;
iTemp = short(Data[10]) << 8 | short(Data[11]); // Probe 1 Alarm Temperature
Temperature[1] = float(iTemp) / 100.0;
iTemp = short(Data[13]) << 8 | short(Data[14]); // Probe 2 Temperature
Temperature[2] = float(iTemp) / 100.0;
iTemp = short(Data[15]) << 8 | short(Data[16]); // Probe 2 Alarm Temperature
Temperature[3] = float(iTemp) / 100.0;
Humidity = 0;
Battery = int(Data[5] & 0x7f);
Averages = 1;
time(&Time);
for (unsigned long index = 0; index < (sizeof(Temperature) / sizeof(Temperature[0])); index++)
TemperatureMin[index] = TemperatureMax[index] = Temperature[index]; //HACK: make sure that these values are set
rval = IsValid();
}
else if (Data.size() == 20) // I'm not checking the Manufacturer data because it appears to be part of the Bluetooth Address on this device
{
// GVH 5055 sample data
//[ ] [A4:C1:38:85:8B:A4] UUIDs: 00005550-0000-1000-8000-00805f9b34fb
//alarms set at 0x31, 0x36, 0x3c, 0x42, 0x4d, 0x5d
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
// probe 1
// [ ] [A4:C1:38:85:8B:A4] ManufacturerData: 8b85:a4 0064 0100 1a00 ffff 3100 01 ffff ffff 3600 0000
// probe 2
// [ ] [A4:C1:38:85:8B:A4] ManufacturerData: 8b85:a4 0064 0200 ffff ffff 3100 01 1b00 ffff 3600 0000
// probe 3
// [ ] [A4:C1:38:85:8B:A4] ManufacturerData: 8b85:a4 0064 4400 1a00 ffff 3c00 00 ffff ffff 4200 0000
// probe 4
// [ ] [A4:C1:38:85:8B:A4] ManufacturerData: 8b85:a4 0064 4800 ffff ffff 3c00 00 1a00 ffff 4200 0000
// probe 5
// [ ] [A4:C1:38:85:8B:A4] ManufacturerData: 8b85:a4 0064 9000 1a00 ffff 4d00 0c ffff ffff 5d00 0000
// probe 6
// [ ] [A4:C1:38:85:8B:A4] ManufacturerData: 8b85:a4 0064 a000 ffff ffff 4d00 0c 1900 ffff 5d00 0000
// It's possible that Byte 11 indicates which probe data is being sent, 0x01:1-2, 0x00:3-4, 0x0c:5-6
//if (Data[11] == 0x01)
{
short iTemp = short(Data[6]) << 8 | short(Data[5]); // Probe 1 Temperature
Temperature[0] = float(iTemp);
iTemp = short(Data[8]) << 8 | short(Data[7]); // Probe 1 Low Alarm Temperature
iTemp = short(Data[10]) << 8 | short(Data[9]); // Probe 1 High Alarm Temperature
Temperature[1] = float(iTemp);
iTemp = short(Data[13]) << 8 | short(Data[12]); // Probe 2 Temperature
Temperature[2] = float(iTemp);
iTemp = short(Data[15]) << 8 | short(Data[14]); // Probe 2 Low Alarm Temperature
iTemp = short(Data[17]) << 8 | short(Data[16]); // Probe 2 High Alarm Temperature
Temperature[3] = float(iTemp);
Humidity = 0;
Battery = int(Data[2]);
Averages = 1;
time(&Time);
for (unsigned long index = 0; index < (sizeof(Temperature) / sizeof(Temperature[0])); index++)
TemperatureMin[index] = TemperatureMax[index] = Temperature[index]; //HACK: make sure that these values are set
rval = IsValid();
}
}
}
return(rval);
}
void Govee_Temp::SetMinMax(const Govee_Temp& a)
{
for (unsigned long index = 0; index < (sizeof(Temperature) / sizeof(Temperature[0])); index++)
{
TemperatureMin[index] = TemperatureMin[index] < Temperature[index] ? TemperatureMin[index] : Temperature[index];
TemperatureMax[index] = TemperatureMax[index] > Temperature[index] ? TemperatureMax[index] : Temperature[index];
TemperatureMin[index] = TemperatureMin[index] < a.TemperatureMin[index] ? TemperatureMin[index] : a.TemperatureMin[index];
TemperatureMax[index] = TemperatureMax[index] > a.TemperatureMax[index] ? TemperatureMax[index] : a.TemperatureMax[index];
}
HumidityMin = HumidityMin < Humidity ? HumidityMin : Humidity;
HumidityMax = HumidityMax > Humidity ? HumidityMax : Humidity;
HumidityMin = HumidityMin < a.HumidityMin ? HumidityMin : a.HumidityMin;
HumidityMax = HumidityMax > a.HumidityMax ? HumidityMax : a.HumidityMax;
}
void Govee_Temp::NormalizeTime(granularity type)
{
if (type == day)
Time = (Time / DAY_SAMPLE) * DAY_SAMPLE;
else if (type == week)
Time = (Time / WEEK_SAMPLE) * WEEK_SAMPLE;
else if (type == month)
Time = (Time / MONTH_SAMPLE) * MONTH_SAMPLE;
else if (type == year)
{
struct tm UTC;
if (0 != localtime_r(&Time, &UTC))
{
UTC.tm_hour = 0;
UTC.tm_min = 0;
UTC.tm_sec = 0;
Time = mktime(&UTC);
}
}
}
Govee_Temp::granularity Govee_Temp::GetTimeGranularity(void) const
{
granularity rval = granularity::day;
struct tm UTC;
if (0 != localtime_r(&Time, &UTC))
{
//if (((UTC.tm_hour == 0) && (UTC.tm_min == 0)) || ((UTC.tm_hour == 23) && (UTC.tm_min == 0) && (UTC.tm_isdst == 1)))
if ((UTC.tm_hour == 0) && (UTC.tm_min == 0))
rval = granularity::year;
else if ((UTC.tm_hour % 2 == 0) && (UTC.tm_min == 0))
rval = granularity::month;
else if ((UTC.tm_min == 0) || (UTC.tm_min == 30))
rval = granularity::week;
}
return(rval);
}
Govee_Temp& Govee_Temp::operator +=(const Govee_Temp& b)
{
if (b.IsValid())
{
Time = std::max(Time, b.Time); // Use the maximum time (newest time)
for (unsigned long index = 0; index < (sizeof(Temperature) / sizeof(Temperature[0])); index++)
{
Temperature[index] = ((Temperature[index] * Averages) + (b.Temperature[index] * b.Averages)) / (Averages + b.Averages);
TemperatureMin[index] = std::min(std::min(Temperature[index], TemperatureMin[index]), b.TemperatureMin[index]);
TemperatureMax[index] = std::max(std::max(Temperature[index], TemperatureMax[index]), b.TemperatureMax[index]);
}
Humidity = ((Humidity * Averages) + (b.Humidity * b.Averages)) / (Averages + b.Averages);
HumidityMin = std::min(std::min(Humidity, HumidityMin), b.HumidityMin);
HumidityMax = std::max(std::max(Humidity, HumidityMax), b.HumidityMax);
Battery = std::min(Battery, b.Battery);
Averages += b.Averages; // existing average + new average
Model = b.Model; // This is important in case "a" was initialized but not valid
}
return(*this);
}
/////////////////////////////////////////////////////////////////////////////
#ifdef _BLUEZ_HCI_
#endif // _BLUEZ_HCI_
/////////////////////////////////////////////////////////////////////////////
// The following operator was required so I could use the std::map<> to use BlueTooth Addresses as the key
bool operator <(const bdaddr_t &a, const bdaddr_t &b)
{
unsigned long long A = a.b[5];
A = A << 8 | a.b[4];
A = A << 8 | a.b[3];
A = A << 8 | a.b[2];
A = A << 8 | a.b[1];
A = A << 8 | a.b[0];
unsigned long long B = b.b[5];
B = B << 8 | b.b[4];
B = B << 8 | b.b[3];
B = B << 8 | b.b[2];
B = B << 8 | b.b[1];
B = B << 8 | b.b[0];
return(A < B);
}
bool operator ==(const bdaddr_t& a, const bdaddr_t& b)
{
unsigned long long A = a.b[5];
A = A << 8 | a.b[4];
A = A << 8 | a.b[3];
A = A << 8 | a.b[2];
A = A << 8 | a.b[1];
A = A << 8 | a.b[0];
unsigned long long B = b.b[5];
B = B << 8 | b.b[4];
B = B << 8 | b.b[3];
B = B << 8 | b.b[2];
B = B << 8 | b.b[1];
B = B << 8 | b.b[0];
return(A == B);
}
/////////////////////////////////////////////////////////////////////////////
std::string ba2string(const bdaddr_t& TheBlueToothAddress)
{
std::ostringstream oss;
for (auto i = 5; i >= 0; i--)
{
oss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(TheBlueToothAddress.b[i]);
if (i > 0)
oss << ":";
}
return (oss.str());
}
const std::regex BluetoothAddressRegex("((([[:xdigit:]]{2}:){5}))[[:xdigit:]]{2}");
bdaddr_t string2ba(const std::string& TheBlueToothAddressString)
{
bdaddr_t TheBlueToothAddress({ 0 });
if (TheBlueToothAddressString.length() == 12)
{
std::string ssBTAddress(TheBlueToothAddressString);
for (auto index = ssBTAddress.length() - 2; index > 0; index -= 2)
ssBTAddress.insert(index, ":");
TheBlueToothAddress = string2ba(ssBTAddress); // Recursive call
}
else if (std::regex_match(TheBlueToothAddressString, BluetoothAddressRegex))
{
std::stringstream ss(TheBlueToothAddressString);
std::string byteString;
int index(5);
// Because I've verified the string format with regex I can safely run this loop knowing it'll get 6 bytes
while (std::getline(ss, byteString, ':'))
TheBlueToothAddress.b[index--] = static_cast<uint8_t>(std::stoi(byteString, nullptr, 16));
}
return(TheBlueToothAddress);
}
/////////////////////////////////////////////////////////////////////////////
std::map<bdaddr_t, std::queue<Govee_Temp>> GoveeTemperatures;
std::map<bdaddr_t, ThermometerType> GoveeThermometers;
std::map<bdaddr_t, time_t> GoveeLastDownload;
const std::filesystem::path GVHLastDownloadFileName("gvh-lastdownload.txt");
/////////////////////////////////////////////////////////////////////////////
volatile bool bRun = true; // This is declared volatile so that the compiler won't optimized it out of loops later in the code
void SignalHandlerSIGINT(int signal)
{
bRun = false;
std::cerr << "***************** SIGINT: Caught Ctrl-C, finishing loop and quitting. *****************" << std::endl;
}
void SignalHandlerSIGHUP(int signal)
{
bRun = false;
std::cerr << "***************** SIGHUP: Caught HangUp, finishing loop and quitting. *****************" << std::endl;
}
void SignalHandlerSIGALRM(int signal)
{
if (ConsoleVerbosity > 0)
std::cout << "[" << getTimeISO8601(true) << "] ***************** SIGALRM: Caught Alarm. *****************" << std::endl;
}
/////////////////////////////////////////////////////////////////////////////
bool ValidateDirectory(const std::filesystem::path& DirectoryName)
{
bool rval = false;
// https://linux.die.net/man/2/stat
struct stat64 StatBuffer;
if (0 == stat64(DirectoryName.c_str(), &StatBuffer))
if (S_ISDIR(StatBuffer.st_mode))
{
// https://linux.die.net/man/2/access
if (0 == access(DirectoryName.c_str(), R_OK | W_OK))
rval = true;
else
{
switch (errno)
{
case EACCES:
std::cerr << DirectoryName << " (" << errno << ") The requested access would be denied to the file, or search permission is denied for one of the directories in the path prefix of pathname." << std::endl;
break;
case ELOOP:
std::cerr << DirectoryName << " (" << errno << ") Too many symbolic links were encountered in resolving pathname." << std::endl;
break;
case ENAMETOOLONG:
std::cerr << DirectoryName << " (" << errno << ") pathname is too long." << std::endl;
break;
case ENOENT:
std::cerr << DirectoryName << " (" << errno << ") A component of pathname does not exist or is a dangling symbolic link." << std::endl;
break;
case ENOTDIR:
std::cerr << DirectoryName << " (" << errno << ") A component used as a directory in pathname is not, in fact, a directory." << std::endl;
break;
case EROFS:
std::cerr << DirectoryName << " (" << errno << ") Write permission was requested for a file on a read-only file system." << std::endl;
break;
case EFAULT:
std::cerr << DirectoryName << " (" << errno << ") pathname points outside your accessible address space." << std::endl;
break;
case EINVAL:
std::cerr << DirectoryName << " (" << errno << ") mode was incorrectly specified." << std::endl;
break;
case EIO:
std::cerr << DirectoryName << " (" << errno << ") An I/O error occurred." << std::endl;
break;
case ENOMEM:
std::cerr << DirectoryName << " (" << errno << ") Insufficient kernel memory was available." << std::endl;
break;
case ETXTBSY:
std::cerr << DirectoryName << " (" << errno << ") Write access was requested to an executable which is being executed." << std::endl;
break;
default:
std::cerr << DirectoryName << " (" << errno << ") An unknown error." << std::endl;
}
}
}
return(rval);
}
// Create a standardized logfile name for this program based on a Bluetooth address and the global parameter of the log file directory.
std::filesystem::path GenerateLogFileName(const bdaddr_t &a, time_t timer = 0)
{
std::ostringstream OutputFilename;
// Original version of filename was formatted gvh507x_XXXX with only last two bytes of bluetooth address
// Second version of filename was formatted gvh507x_XXXXXXXXXXXX with all six bytes of the bluetooth address
// Third version of filename is formatted gvh-XXXXXXXXXXXX because I've been tired of the 507x for the past two years (2023-04-03)
//OutputFilename << "gvh507x_";
//OutputFilename << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << int(a.b[1]);
//OutputFilename << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << int(a.b[0]);
// The New Format Log File Name includes the entire Bluetooth Address, making it much easier to recognize and add to MRTG config files.
OutputFilename << "gvh-";
std::string btAddress(ba2string(a));
for (auto pos = btAddress.find(':'); pos != std::string::npos; pos = btAddress.find(':'))
btAddress.erase(pos, 1);
OutputFilename << btAddress;
if (timer == 0)
time(&timer);
struct tm UTC;
if (0 != gmtime_r(&timer, &UTC))
if (!((UTC.tm_year == 70) && (UTC.tm_mon == 0) && (UTC.tm_mday == 1)))
OutputFilename << "-" << std::dec << UTC.tm_year + 1900 << "-" << std::setw(2) << std::setfill('0') << UTC.tm_mon + 1;
OutputFilename << ".txt";
std::filesystem::path NewFormatFileName(LogDirectory / OutputFilename.str());
return(NewFormatFileName);
}
bool GenerateLogFile(std::map<bdaddr_t, std::queue<Govee_Temp>> &AddressTemperatureMap, std::map<bdaddr_t, time_t> &PersistenceData)