-
Notifications
You must be signed in to change notification settings - Fork 15
/
model.py
1343 lines (1198 loc) · 57.8 KB
/
model.py
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
from peewee import *
from playhouse.pool import PooledMySQLDatabase
from playhouse.shortcuts import RetryOperationalError
import datetime
from math import radians, cos, sin, asin, sqrt
class MyRetryDatabase(RetryOperationalError, MySQLDatabase):
commit_select = False
# MySQL Database cfg
db = MyRetryDatabase(
'', # Database
host='',
port=3306,
user='',
passwd='',
charset='utf8mb4',
threadlocals=True)
class BaseModel(Model):
class Meta:
database = db
class Camera(BaseModel):
id = CharField(primary_key=True)
mac = CharField()
wifimac = CharField()
deviceid = CharField()
retry = IntegerField(default=0)
creation_date = DateTimeField(default=datetime.datetime.now)
class Meta:
order_by = ('id',)
def _create(mac, wifimac, deviceid):
cam_id = Camera.calc_id(mac, wifimac, deviceid)
cam = Camera._get(cam_id)
if cam:
return cam.set_online()
camera = None
try:
with db.execution_context() as ctx:
# Attempt to create the camera. If the id is taken, due to the
# unique constraint, the database will raise an IntegrityError.
camera = Camera.create(
id=cam_id,
# replace('-', ':') was not supposed to be necessary, but...
mac=mac.replace('-', ':').upper(),
# replace('-', ':') was not supposed to be necessary, but...
wifimac=wifimac.replace('-', ':').upper(),
deviceid=deviceid
)
except IntegrityError:
pass
return camera
def _get(id):
camera = None
try:
with db.execution_context() as ctx:
camera = Camera.get(Camera.id == id)
except Camera.DoesNotExist:
pass
return camera
def _get_by_ip(ip):
camera = None
try:
with db.execution_context() as ctx:
subquery = Address.select(Address.camera).where(
Address.ip == ip).order_by(Address.date_id.desc()).limit(1)
camera = Camera.get(Camera.id == subquery)
except Camera.DoesNotExist:
pass
return camera
def _get_by_addr(ip, port):
camera = None
try:
with db.execution_context() as ctx:
subquery = Address.select(Address.camera).where((Address.ip == ip) & (
Address.port == port)).order_by(Address.date_id.desc()).limit(1)
camera = Camera.get(Camera.id == subquery)
except Camera.DoesNotExist:
pass
return camera
def set_online(self):
try:
with db.execution_context() as ctx:
# Attempt to set a camera online. If the camera doesn't exist
# the database will raise an IntegrityError.
self.retry = 0
self.save()
except IntegrityError:
pass
return self
def set_offline(self):
try:
with db.execution_context() as ctx:
# Attempt to set a camera offline. If the camera doesn't exist
# the database will raise an IntegrityError.
self.retry += 1
self.save()
except IntegrityError:
pass
return self
def get_creds(self):
creds = None
try:
with db.execution_context() as ctx:
creds = Credentials.select().where(Credentials.camera == self).order_by(
Credentials.date_id.desc()).get()
except Credentials.DoesNotExist:
pass
return creds
def get_creds_hist(self):
creds = None
try:
with db.execution_context() as ctx:
creds = list(Credentials.select().distinct().where(
Credentials.camera == self))
except Credentials.DoesNotExist:
pass
return creds
def get_addr(self):
address = None
try:
with db.execution_context() as ctx:
address = Address.select().where(
Address.camera == self).order_by(Address.date_id.desc()).get()
except Address.DoesNotExist:
pass
return address
def get_addr_hist(self):
address = None
try:
with db.execution_context() as ctx:
address = list(Address.select().distinct().where(
Address.camera == self))
except Address.DoesNotExist:
pass
return address
def get_ddns(self):
ddns = None
try:
with db.execution_context() as ctx:
ddns = DDNS.select().where(DDNS.camera == self).order_by(DDNS.date_id.desc()).get()
except DDNS.DoesNotExist:
pass
return ddns
def get_ddns_hist(self):
ddns = None
try:
with db.execution_context() as ctx:
ddns = list(DDNS.select().distinct().where(
DDNS.camera == self))
except DDNS.DoesNotExist:
pass
return ddns
def get_ftp(self):
ftp = None
try:
with db.execution_context() as ctx:
ftp = FTP.select().where(FTP.camera == self).order_by(FTP.date_id.desc()).get()
except FTP.DoesNotExist:
pass
return ftp
def get_ftp_hist(self):
ftp = None
try:
with db.execution_context() as ctx:
ftp = list(FTP.select().distinct().where(FTP.camera == self))
except FTP.DoesNotExist:
pass
return ftp
def get_loc(self, det=3):
loc = None
try:
with db.execution_context() as ctx:
loc = Location.select().where((Location.camera == self) & (
Location.detail < det)).order_by(Location.date_id.desc()).get()
except Location.DoesNotExist:
pass
return loc
def get_loc_hist(self):
loc = None
try:
with db.execution_context() as ctx:
loc = list(Location.select().distinct().where(
Location.camera == self))
except Location.DoesNotExist:
pass
return loc
def get_mail(self):
mail = None
try:
with db.execution_context() as ctx:
mail = Mail.select().where(Mail.camera == self).order_by(Mail.date_id.desc()).get()
except Mail.DoesNotExist:
pass
return mail
def get_mail_hist(self):
mail = None
try:
with db.execution_context() as ctx:
mail = list(Mail.select().distinct().where(
Mail.camera == self))
except Mail.DoesNotExist:
pass
return mail
def get_status(self):
status = None
try:
with db.execution_context() as ctx:
status = Status.select().where(Status.camera == self).order_by(
Status.date_id.desc()).get()
except Status.DoesNotExist:
pass
return status
def get_status_hist(self):
status = None
try:
with db.execution_context() as ctx:
status = list(Status.select().distinct().where(
Status.camera == self))
except Status.DoesNotExist:
pass
return status
def get_smarteye(self):
smarteye = None
try:
with db.execution_context() as ctx:
smarteye = SmartEye.select().where(
SmartEye.camera == self).order_by(SmartEye.date_id.desc()).get()
except SmartEye.DoesNotExist:
pass
return smarteye
def get_smarteye_hist(self):
status = None
try:
with db.execution_context() as ctx:
smarteye = list(SmartEye.select().distinct().where(
SmartEye.camera == self))
except SmartEye.DoesNotExist:
pass
return smarteye
def get_wifiscan(self):
wifi_scan = None
try:
with db.execution_context() as ctx:
wifi_scan = Wifi_scan.select().where(
Wifi_scan.camera == self).order_by(Wifi_scan.date_id.desc()).get()
except Wifi_scan.DoesNotExist:
pass
return wifi_scan
def get_wifiscan_hist(self):
wifi_scan = None
try:
with db.execution_context() as ctx:
wifi_scan = list(Wifi_scan.select().distinct().where(
Wifi_scan.camera == self))
except Wifi_scan.DoesNotExist:
pass
return wifi_scan
def get_wifiap(self):
wifi_ap = None
try:
wifi_ap = Wifi_AP.select().join(Wifi_scan).where(
Wifi_scan.camera == self).order_by(Wifi_scan.date_id.desc()).get()
except Wifi_AP.DoesNotExist:
pass
return wifi_ap
def get_wifiap_hist(self):
wifi_ap = None
try:
with db.execution_context() as ctx:
wifi_ap = list(Wifi_AP.select().distinct().join(
Wifi_scan).where(Wifi_scan.camera == self))
except Wifi_AP.DoesNotExist:
pass
return wifi_ap
def get_last(self):
ts = None
try:
with db.execution_context() as ctx:
ts = Timestamp.select().join(Wifi_scan).where(Wifi_scan.camera ==
self).order_by(Wifi_scan.date_id.desc()).get()
except Timestamp.DoesNotExist:
pass
return ts
def get_last_hist(self):
ts = None
try:
with db.execution_context() as ctx:
ts = list(Timestamp.select().distinct().join(
Wifi_scan).where(Wifi_scan.camera == self))
except Timestamp.DoesNotExist:
pass
return ts
def _get_online():
cameras = None
try:
with db.execution_context() as ctx:
cameras = list(Camera.select().where(Camera.retry < 3))
except Camera.DoesNotExist:
pass
return cameras
def _get_outdated(hh=12):
datetime_span = datetime.datetime.now() - datetime.timedelta(hours=hh)
cameras = None
try:
with db.execution_context() as ctx:
subquery = Camera.select(Camera.id).distinct().join(Wifi_scan).join(
Timestamp).where((Camera.retry < 3) & (Timestamp.date > datetime_span))
cameras = list(Camera.select().distinct().join(Address).where((Camera.retry < 3) & ~(
Camera.id << subquery)).order_by(fn.Rand())) # .order_by(fn.Random()) for Postgresql and Sqlite
except Camera.DoesNotExist:
pass
return cameras
def _get_outdated_dict(hh=12):
datetime_span = datetime.datetime.now() - datetime.timedelta(hours=hh)
mydict = None
try:
with db.execution_context() as ctx:
subquery1 = Camera.select(Camera.id).distinct().join(Wifi_scan).join(
Timestamp).where((Camera.retry < 3) & (Timestamp.date > datetime_span))
subquery2 = Camera.select(Camera.id.alias('camid')).annotate(Address, fn.Max(Address.id).alias(
'addrid')).where((Camera.retry < 3) & ~(Camera.id << subquery1)).alias('subquery2')
mydict = list(Camera.select(Camera.id, Address.ip, Address.port).join(subquery2, on=(Camera.id == subquery2.c.camid).alias('cam')).join(
Address, on=(Address.id == subquery2.c.addrid).alias('addr')).order_by(fn.Rand()).dicts()) # .order_by(fn.Random()) for Postgresql and Sqlite
except Camera.DoesNotExist:
pass
return mydict
def _get_located(det=2):
cameras = None
try:
with db.execution_context() as ctx:
cameras = list(Camera.select().distinct().join(Location).where((Camera.retry < 3) & (
Location.detail < det)).order_by(fn.Rand())) # .order_by(fn.Random()) for Postgresql and Sqlite
except Camera.DoesNotExist:
pass
return cameras
def _get_located_dict(det=2, **kwargs):
mydict = None
try:
with db.execution_context() as ctx:
camquery = Camera.select(Camera.id.alias(
'camid')).where(Camera.retry < 3)
subquery = Camera.select(Camera.id.alias('camid')).annotate(Address, fn.Max(Address.id).alias('addrid')).annotate(Credentials, fn.Max(
Credentials.id).alias('credid')).annotate(Location, fn.Max(Location.id).alias('locid')).where(Camera.id << camquery).alias('subquery')
if 'country' in kwargs:
mydict = list(Camera.select(Camera.deviceid, Camera.mac, Camera.wifimac, Camera.retry, Address.ip, Address.port, Credentials.user_admin, Credentials.passwd_admin, Location.lat, Location.lng, Location.addr_formatted).join(subquery, on=(Camera.id == subquery.c.camid).alias('cam')).join(Address, on=(Address.id == subquery.c.addrid).alias(
'addr')).join(Credentials, on=(Credentials.id == subquery.c.credid).alias('creds')).join(Location, on=(Location.id == subquery.c.locid).alias('loc')).where((Location.detail < 1) & (Location.addrcountry == kwargs['country'])).order_by(fn.Rand()).dicts())
# .order_by(fn.Random()) for Postgresql and Sqlite
else:
mydict = list(Camera.select(Camera.deviceid, Camera.mac, Camera.wifimac, Camera.retry, Address.ip, Address.port, Credentials.user_admin, Credentials.passwd_admin, Location.lat, Location.lng, Location.addr_formatted).join(subquery, on=(Camera.id == subquery.c.camid).alias('cam')).join(Address, on=(
Address.id == subquery.c.addrid).alias('addr')).join(Credentials, on=(Credentials.id == subquery.c.credid).alias('creds')).join(Location, on=(Location.id == subquery.c.locid).alias('loc')).where(Location.detail < det).order_by(fn.Rand()).dicts())
# .order_by(fn.Random()) for Postgresql and Sqlite
except Camera.DoesNotExist:
pass
return mydict
def _get_all_located_dict(det=2, **kwargs):
mydict = None
try:
with db.execution_context() as ctx:
subquery = Camera.select(Camera.id.alias('camid')).annotate(Address, fn.Max(Address.id).alias('addrid')).annotate(
Credentials, fn.Max(Credentials.id).alias('credid')).annotate(Location, fn.Max(Location.id).alias('locid')).alias('subquery')
if 'country' in kwargs:
mydict = list(Camera.select(Camera.deviceid, Camera.mac, Camera.wifimac, Camera.retry, Address.ip, Address.port, Credentials.user_admin, Credentials.passwd_admin, Location.lat, Location.lng, Location.addr_formatted).join(subquery, on=(Camera.id == subquery.c.camid).alias('cam')).join(Address, on=(Address.id == subquery.c.addrid).alias(
'addr')).join(Credentials, on=(Credentials.id == subquery.c.credid).alias('creds')).join(Location, on=(Location.id == subquery.c.locid).alias('loc')).where((Location.detail < 1) & (Location.addrcountry == kwargs['country'])).order_by(fn.Rand()).dicts())
# .order_by(fn.Random()) for Postgresql and Sqlite
else:
mydict = list(Camera.select(Camera.deviceid, Camera.mac, Camera.wifimac, Camera.retry, Address.ip, Address.port, Credentials.user_admin, Credentials.passwd_admin, Location.lat, Location.lng, Location.addr_formatted).join(subquery, on=(Camera.id == subquery.c.camid).alias('cam')).join(Address, on=(
Address.id == subquery.c.addrid).alias('addr')).join(Credentials, on=(Credentials.id == subquery.c.credid).alias('creds')).join(Location, on=(Location.id == subquery.c.locid).alias('loc')).where(Location.detail < det).order_by(fn.Rand()).dicts())
# .order_by(fn.Random()) for Postgresql and Sqlite
except Camera.DoesNotExist:
pass
return mydict
###Statistics###
def _get_distinct_deviceid_stats(retry=None):
cameras = None
try:
with db.execution_context() as ctx:
if retry is None or retry < 0:
cameras = Camera.select(
fn.Count(fn.Distinct(Camera.deviceid))).scalar()
else:
cameras = Camera.select(fn.Count(fn.Distinct(Camera.deviceid))).where(
Camera.retry < retry + 1).scalar()
except Camera.DoesNotExist:
pass
return cameras
def _get_located_stats_dict(det=2):
mydict = None
try:
with db.execution_context() as ctx:
camquery = Camera.select(Camera.id.alias(
'camid')).where(Camera.retry < 3)
subquery = Camera.select(Camera.id.alias('camid')).annotate(Address, fn.Max(Address.id).alias('addrid')).annotate(Credentials, fn.Max(
Credentials.id).alias('credid')).annotate(Location, fn.Max(Location.id).alias('locid')).where(Camera.id << camquery).alias('subquery')
mydict = list(Camera.select(Location.addr_country.alias('country'), fn.Count(Camera.id).alias('camera')).join(subquery, on=(Camera.id == subquery.c.camid).alias('cam')).join(Address, on=(Address.id == subquery.c.addrid).alias('addr')).join(
Credentials, on=(Credentials.id == subquery.c.credid).alias('creds')).join(Location, on=(Location.id == subquery.c.locid).alias('loc')).where(Location.detail < det).group_by(SQL('country')).order_by(SQL('camera').desc()).dicts())
mydict = {d['country']: d['camera'] for d in mydict}
except Camera.DoesNotExist:
pass
return mydict
def _get_all_located_stats_dict(det=2):
mydict = None
try:
with db.execution_context() as ctx:
subquery = Camera.select(Camera.id.alias('camid')).annotate(Address, fn.Max(Address.id).alias('addrid')).annotate(
Credentials, fn.Max(Credentials.id).alias('credid')).annotate(Location, fn.Max(Location.id).alias('locid')).alias('subquery')
mydict = list(Camera.select(Location.addr_country.alias('country'), fn.Count(Camera.id).alias('camera')).join(subquery, on=(Camera.id == subquery.c.camid).alias('cam')).join(Address, on=(Address.id == subquery.c.addrid).alias('addr')).join(
Credentials, on=(Credentials.id == subquery.c.credid).alias('creds')).join(Location, on=(Location.id == subquery.c.locid).alias('loc')).where(Location.detail < det).group_by(SQL('country')).order_by(SQL('camera').desc()).dicts())
mydict = {d['country']: d['camera'] for d in mydict}
except Camera.DoesNotExist:
pass
return mydict
def calc_id(mac, wifimac, deviceid):
macs = [mac, wifimac]
for i in range(len(macs)):
# replace('-', '') was not supposed to be necessary, but...
macs[i] = macs[i].replace(':', '').replace('-', '')
if not macs[i]:
macs[i] = '0'
macs[i] = int(str(macs[i]), 16)
return('%s-%s-%s' % (macs[0], macs[1], deviceid))
def cmp(self, camera_candidate): # compare
return self.id == camera_candidate.id
class Timestamp(BaseModel):
date = DateTimeField(default=datetime.datetime.now)
class Meta:
order_by = ('-date',)
def _create():
try:
with db.execution_context() as ctx:
# Attempt to create the timestamp.
date = Timestamp.create()
except IntegrityError:
pass
return date
###Statistics###
def _get_stats(hh=None):
dates = None
try:
with db.execution_context() as ctx:
if hh is None or hh < 0:
dates = Timestamp.select().count()
else:
datetime_span = datetime.datetime.now() - datetime.timedelta(hours=hh)
dates = Timestamp.select().where(Timestamp.date > datetime_span).count()
except Timestamp.DoesNotExist:
pass
return dates
class Credentials(BaseModel):
camera = ForeignKeyField(Camera, related_name='creds_belongs_to')
date = ForeignKeyField(Timestamp, related_name='creds_at')
user_admin = CharField()
passwd_admin = CharField()
user_mod = CharField()
passwd_mod = CharField()
user_guest = CharField()
passwd_guest = CharField()
class Meta:
order_by = ('camera', '-date')
def _create(camera, date, user_a, passwd_a, user_m, passwd_m, user_g, passwd_g):
try:
with db.execution_context() as ctx:
# Attempt to create the credentials.
credentials = Credentials.create(
camera=camera,
date=date,
user_admin=user_a,
passwd_admin=passwd_a,
user_mod=user_m,
passwd_mod=passwd_m,
user_guest=user_g,
passwd_guest=passwd_g
)
except IntegrityError:
pass
return credentials
def _create_dict(camera, date, cred_dict):
try:
with db.execution_context() as ctx:
# Attempt to create the credentials.
credentials = Credentials.create(
camera=camera,
date=date,
user_admin=cred_dict.get('3_name', ''),
passwd_admin=cred_dict.get('3_pwd', ''),
user_mod=cred_dict.get('2_name', ''),
passwd_mod=cred_dict.get('2_pwd', ''),
user_guest=cred_dict.get('1_name', ''),
passwd_guest=cred_dict.get('1_pwd', '')
)
except IntegrityError:
pass
return credentials
###Statistics###
def _get_stats(limit=50):
credentials = None
try:
with db.execution_context() as ctx:
subquery = Camera.select(Camera.id.alias('camid')).annotate(
Credentials, fn.Max(Credentials.id).alias('credid')).alias('subquery')
user = list(Credentials.select(Credentials.user_admin.alias('user'), fn.Count(Credentials.id).alias('rep')).join(subquery, on=(
Credentials.id == subquery.c.credid).alias('credid')).group_by(SQL('user')).order_by(SQL('rep').desc()).limit(limit).dicts())
passwd = list(Credentials.select(Credentials.passwd_admin.alias('passwd'), fn.Count(Credentials.id).alias('rep')).join(subquery, on=(
Credentials.id == subquery.c.credid).alias('credid')).group_by(SQL('passwd')).order_by(SQL('rep').desc()).limit(limit).dicts())
credentials = {'user': {d['user']: d['rep'] for d in user}, 'passwd': {
d['passwd']: d['rep'] for d in passwd}}
except Credentials.DoesNotExist:
pass
return credentials
def cmp(self, user_a, passwd_a, user_m, passwd_m, user_g, passwd_g): # compare
return self.user_admin == user_a and self.passwd_admin == passwd_a and self.user_mod == user_m and self.passwd_mod == passwd_m and \
self.user_guest == user_g and self.passwd_guest == passwd_g
def cmp_dict(self, cred_dict): # compare
return self.user_admin == cred_dict.get('3_name', '') and self.passwd_admin == cred_dict.get('3_pwd', '') and self.user_mod == cred_dict.get('2_name', '') and \
self.passwd_mod == cred_dict.get('2_pwd', '') and self.user_guest == cred_dict.get(
'1_name', '') and self.passwd_guest == cred_dict.get('1_pwd', '')
class Address(BaseModel):
camera = ForeignKeyField(Camera, related_name='addr_belongs_to')
date = ForeignKeyField(Timestamp, related_name='addr_at')
ip = CharField()
port = IntegerField()
class Meta:
order_by = ('camera', '-date')
def _create(camera, date, ip, port):
try:
with db.execution_context() as ctx:
# Attempt to create the address.
address = Address.create(
camera=camera,
date=date,
ip=ip,
port=port
)
except IntegrityError:
pass
return address
###Statistics###
def _get_stats(limit=50):
address = None
try:
with db.execution_context() as ctx:
subquery = Camera.select(Camera.id.alias('camid')).annotate(
Address, fn.Max(Address.id).alias('addrid')).alias('subquery')
ip_prefix = list(Address.select(fn.substring_index(Address.ip, '.', 2).alias('ip_prefix'), fn.Count(Address.id).alias('rep')).join(
subquery, on=(Address.id == subquery.c.addrid).alias('addrid')).group_by(SQL('ip_prefix')).order_by(SQL('rep').desc()).limit(limit).dicts())
port = list(Address.select(Address.port.alias('port'), fn.Count(Address.id).alias('rep')).join(subquery, on=(
Address.id == subquery.c.addrid).alias('addrid')).group_by(SQL('port')).order_by(SQL('rep').desc()).limit(limit).dicts())
address = {'ip_prefix': {d['ip_prefix']: d['rep'] for d in ip_prefix}, 'port': {
d['port']: d['rep'] for d in port}}
except Address.DoesNotExist:
pass
return address
def cmp(self, ip, port): # compare
return self.ip == ip and self.port == port
class DDNS(BaseModel):
camera = ForeignKeyField(Camera, related_name='ddns_belongs_to')
date = ForeignKeyField(Timestamp, related_name='ddns_at')
service = IntegerField()
host = CharField()
user = CharField()
passwd = CharField()
proxy_host = CharField()
proxy_port = IntegerField()
status = IntegerField()
def _create(camera, date, service, host, user, passwd, proxy_host, proxy_port, status):
try:
with db.execution_context() as ctx:
# Attempt to create the ddns.
ddns = DDNS.create(
camera=camera,
date=date,
service=int(service),
host=host,
user=user,
passwd=passwd,
proxy_host=proxy_host,
proxy_port=int(proxy_port),
status=int(status)
)
except IntegrityError:
pass
return ddns
def _create_dict(camera, date, ddns_dict):
try:
with db.execution_context() as ctx:
# Attempt to create the ddns with dict.
ddns = DDNS.create(
camera=camera,
date=date,
service=int(ddns_dict.get('service', '0')),
host=ddns_dict.get('host', ''),
user=ddns_dict.get('user', ''),
passwd=ddns_dict.get('pwd', ''),
proxy_host=ddns_dict.get('proxy_svr', ''),
proxy_port=int(ddns_dict.get('proxy_port', '0')),
status=int(ddns_dict.get('status', '0'))
)
except IntegrityError:
pass
return ddns
def cmp(self, service, host, user, passwd, proxy_host, proxy_port, status): # compare
return self.service == int(service) and self.host == host and self.user == user and self.passwd == passwd and \
self.proxy_host == proxy_host and self.proxy_port == int(
proxy_port) and self.status == int(status)
def cmp_dict(self, ddns_dict): # compare
return self.service == int(ddns_dict.get('service', '0')) and self.host == ddns_dict.get('host', '') and self.user == ddns_dict.get('user', '') and \
self.passwd == ddns_dict.get('pwd', '') and self.proxy_host == ddns_dict.get('proxy_host', '') and \
self.proxy_port == int(ddns_dict.get('proxy_port', '0')) and self.status == int(
ddns_dict.get('status', '0'))
class FTP(BaseModel):
camera = ForeignKeyField(Camera, related_name='ftp_belongs_to')
date = ForeignKeyField(Timestamp, related_name='ftp_at')
host = CharField()
port = IntegerField()
user = CharField()
passwd = CharField()
path = CharField()
mode = BooleanField()
upload_interval = IntegerField()
def _create(camera, date, host, port, user, passwd, path, mode, upload_interval):
try:
with db.execution_context() as ctx:
# Attempt to create the ftp.
ftp = FTP.create(
camera=camera,
date=date,
host=host,
port=int(port),
user=user,
passwd=passwd,
path=path,
mode=str(mode) == '1',
upload_interval=int(upload_interval)
)
except IntegrityError:
pass
return ftp
def _create_dict(camera, date, ftp_dict):
try:
with db.execution_context() as ctx:
# Attempt to create the cftp with a dict.
ftp = FTP.create(
camera=camera,
date=date,
host=ftp_dict.get('svr', ''),
port=int(ftp_dict.get('port', '0')),
user=ftp_dict.get('user', ''),
passwd=ftp_dict.get('pwd', ''),
path=ftp_dict.get('dir', ''),
mode=str(ftp_dict.get('mode', '0')) == '1',
upload_interval=int(ftp_dict.get('upload_interval', '0'))
)
except IntegrityError:
pass
return ftp
def cmp(self, host, port, user, passwd, path, mode, upload_interval): # compare
return self.host == host and self.port == int(port) and self.user == user and self.passwd == passwd and self.path == path and \
self.mode == (str(mode) == '1') and self.upload_interval == int(
upload_interval)
def cmp_dict(self, ftp_dict): # compare
return self.host == ftp_dict.get('svr', '') and self.port == int(ftp_dict.get('port', '0')) and self.user == ftp_dict.get('user', '') and \
self.passwd == ftp_dict.get('pwd', '') and self.path == ftp_dict.get('dir', '') and \
self.mode == (str(ftp_dict.get('mode', '0')) == '1') and self.upload_interval == int(
ftp_dict.get('upload_interval', '0'))
class Location(BaseModel):
camera = ForeignKeyField(Camera, related_name='loc_belongs_to')
date = ForeignKeyField(Timestamp, related_name='loc_at')
lat = DoubleField(null=True)
lng = DoubleField(null=True)
accuracy = DoubleField(null=True)
max_accuracy = 150
addr_street_number = TextField(null=True)
addr_route = TextField(null=True)
addr_city = TextField(null=True)
addr_region = TextField(null=True)
addr_postal_code = TextField(null=True)
addr_country = TextField()
addr_formatted = TextField(null=True)
# detail [0: geoloc+geocode, 1:geoloc+country(ip based), 2:country(ip based)]
detail = IntegerField()
class Meta:
order_by = ('camera', '-date')
def _create(camera, date, geoloc, addr):
try:
with db.execution_context() as ctx:
# Attempt to create the location with geoloc and a dict.
location = Location.create(
camera=camera,
date=date,
lat=geoloc['lat'],
lng=geoloc['lng'],
accuracy=geoloc['accuracy'],
# street_number, premise, sublocality_level_4
addr_street_number=addr.get('street_number', ''),
# route, sublocality_level_2
addr_route=addr.get('route', ''),
addr_city=addr.get('city', ''), # locality
# administrative_area_level_1
addr_region=addr.get('region', ''),
addr_postal_code=addr.get('postal_code', ''),
addr_country=addr.get('country', ''),
addr_formatted=addr.get('formatted', ''),
detail=0
)
except IntegrityError:
pass
return location
def _create_loc(camera, date, geoloc, country):
try:
with db.execution_context() as ctx:
# Attempt to create the address only with geoloc and country (ip based).
location = Location.create(
camera=camera,
date=date,
lat=geoloc['lat'],
lng=geoloc['lng'],
accuracy=geoloc['accuracy'],
addr_country=country,
detail=1
)
except IntegrityError:
pass
return location
def _create_country(camera, date, country):
try:
with db.execution_context() as ctx:
# Attempt to create the address only with a country (ip based).
location = Location.create(
camera=camera,
date=date,
addr_country=country,
detail=2
)
except IntegrityError:
pass
return location
def distance(self, lat, lng):
# Calculate the great circle distance between two points
# on the earth (specified in decimal degrees)
for i in [self.lat, self.lng]:
if type(i) != int:
return 0
for i in [lat, lng]:
if type(i) != int:
return self.max_accuracy
lat1, lng1, lat2, lng2 = map(radians, [self.lat, self.lng, lat, lng])
dlat = lat2 - lat1
dlon = lng2 - lng1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * asin(sqrt(a))
m = 6367000 * c
return m
###Statistics###
def _get_stats_dict(det=2):
mydict = None
try:
with db.execution_context() as ctx:
subquery = Camera.select(Camera.id.alias('camid')).annotate(
Location, fn.Max(Location.id).alias('locid')).alias('subquery')
locations_raw = list(Location.select(Location.addr_country.alias('country'), Location.addr_region.alias('region'), fn.Count(SQL('*')).alias('rep')).join(
subquery, on=(Location.id == subquery.c.locid).alias('loc')).where(Location.detail < det).group_by(SQL('country, region')).order_by(SQL('rep').desc()).dicts())
mydict = {}
for loc in locations_raw:
loc_country = loc.get('country', '')
if loc_country is None or loc_country == '':
loc_country = 'unkown'
loc_region = loc.get('region', '')
if loc_region is None or loc_region == '':
loc_region = 'unkown'
country = mydict.get(
loc_country, {'total': 0, 'region': {}})
region = country.get('region')
region.update({loc_region: loc.get('rep', 0)})
country.update({'total': country.get(
'total') + loc.get('rep', 0), 'region': region})
mydict.update({loc_country: country})
except Location.DoesNotExist:
pass
return mydict
def cmp(self, lat, lng, accuracy): # Returns false if nos equal or accuracy shitty
if self.accuracy:
# if self.accuracy is not None:
if accuracy < self.max_accuracy:
return self.distance(lat, lng) < min(self.accuracy, accuracy)
else:
return False
else:
return True
def cmp_country(self, country):
return self.addr_country == country
def addr_format(self):
if self.detail == 0:
return "%s %s, %s, %s %s, %s" % (self.addr_street_number, self.addr_route, self.addr_city, self.addr_region, self.addr_postal_code, self.addr_country)
else:
return self.addr_country
class Mail(BaseModel):
camera = ForeignKeyField(Camera, related_name='mail_belongs_to')
date = ForeignKeyField(Timestamp, related_name='mail_at')
email = CharField()
host = CharField()
port = IntegerField()
user = CharField()
passwd = CharField()
ssl = BooleanField()
receiver1 = CharField()
receiver2 = CharField()
receiver3 = CharField()
receiver4 = CharField()
inet_ip = BooleanField()
def _create(camera, date, email, host, port, user, passwd, ssl, receiver1, receiver2, receiver3, receiver4, inet_ip):
receivers = sorted([receiver1, receiver2, receiver3,
receiver4], key=lambda x: (x == "", x.lower()))
try:
with db.execution_context() as ctx:
# Attempt to create the mail.
mail = Mail.create(
camera=camera,
date=date,
email=email,
host=host,
port=int(port),
user=user,
passwd=passwd,
ssl=str(ssl) == '1',
receiver1=receivers[0],
receiver2=receivers[1],
receiver3=receivers[2],
receiver4=receivers[3],
inet_ip=str(inet_ip) == '1'
)
except IntegrityError:
pass
return mail
def _create_dict(camera, date, mail_dict):
receivers = sorted([mail_dict.get('receiver1', ''), mail_dict.get('receiver2', ''), mail_dict.get(
'receiver3', ''), mail_dict.get('receiver4', '')], key=lambda x: (x == "", x.lower()))
try:
with db.execution_context() as ctx:
# Attempt to create the credentials.
mail = Mail.create(
camera=camera,
date=date,
email=mail_dict.get('sender', ''),
host=mail_dict.get('svr', ''),
port=int(mail_dict.get('port', '0')),
user=mail_dict.get('user', ''),
passwd=mail_dict.get('pwd', ''),
ssl=str(mail_dict.get('ssl', '0')) == '1',
receiver1=receivers[0],
receiver2=receivers[1],
receiver3=receivers[2],
receiver4=receivers[3],
inet_ip=str(mail_dict.get('inet_ip', '0')) == '1'
)
except IntegrityError:
pass
return mail
def cmp(self, email, host, port, user, passwd, ssl, receiver1, receiver2, receiver3, receiver4, inet_ip): # compare
receivers = sorted([receiver1, receiver2, receiver3,
receiver4], key=lambda x: (x == "", x.lower()))
return self.email == email and self.host == host and self.port == int(port) and self.user == user and self.passwd == passwd and self.ssl == (str(ssl) == '1') and \
self.receiver1 == receivers[0] and self.receiver2 == receivers[1] and self.receiver3 == receivers[
2] and self.receiver4 == receivers[3] and self.inet_ip == (str(inet_ip) == '1')
def cmp_dict(self, mail_dict): # compare
receivers = sorted([mail_dict.get('receiver1', ''), mail_dict.get('receiver2', ''), mail_dict.get(
'receiver3', ''), mail_dict.get('receiver4', '')], key=lambda x: (x == "", x.lower()))
return self.email == mail_dict.get('sender', '') and self.host == mail_dict.get('svr', '') and self.port == int(mail_dict.get('port', '0')) and \
self.user == mail_dict.get('user', '') and self.passwd == mail_dict.get('pwd', '') and self.ssl == (str(mail_dict.get('ssl', '0')) == '1') and \
self.receiver1 == receivers[0] and self.receiver2 == receivers[1] and self.receiver3 == receivers[2] and self.receiver4 == receivers[3] and \
self.inet_ip == (str(mail_dict.get('inet_ip', '0')) == '1')
class Status(BaseModel):
camera = ForeignKeyField(Camera, related_name='stat_belongs_to')
date = ForeignKeyField(Timestamp, related_name='stat_at')
alias = CharField()
sys_ver = CharField()
app_ver = CharField()
oem_id = CharField()
sd_status = IntegerField()
syswifi_mode = IntegerField()
class Meta:
order_by = ('camera', '-date')
def _create(camera, date, alias, sys_ver, app_ver, oem_id, sd_status, syswifi_mode):
try:
with db.execution_context() as ctx:
# Attempt to create the status.
status = Status.create(
camera=camera,
date=date,
alias=alias,
sys_ver=sys_ver,
app_ver=app_ver,
oem_id=oem_id,
sd_status=int(sd_status),
syswifi_mode=int(syswifi_mode)
)
except IntegrityError:
pass
return status