-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
164 lines (125 loc) · 4.57 KB
/
models.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
""" MODELS MODULE """
import logging
import random
import msgpack
import numpy
from peewee import (SqliteDatabase, Model, CharField, IntegerField, BlobField,
ForeignKeyField, PeeweeException)
from colorama import Style
logging.basicConfig(level=logging.CRITICAL,
format=Style.BRIGHT + "%(asctime)s - %(levelname)s - %(message)s" +
Style.NORMAL)
DB = SqliteDatabase("database.db")
STDV_CORRECTION = 2 # Standard deviation correction factor
COMPLIANT = [1]
###############################################
# DATABASE MODELS
###############################################
class BaseModel(Model):
""" BASE MODEL """
class Meta:
""" META DATA FOR DB IDENTIFIER """
database = DB
class EcuType(BaseModel):
""" ECU TYPE MODEL CLASS """
ecu_name = CharField()
ecu_pincount = IntegerField()
class PinData(BaseModel):
""" PIN DATA MODEL CLASS """
ecu_name = ForeignKeyField(EcuType, backref="ecu")
ecu_ref_number = CharField()
ecu_db_number = IntegerField()
pin_reading_msgpack = BlobField()
#################################################
# FUNCTIONS DEFINITION
#################################################
def uix_input():
""" TEST ROUTINE MANUAL INPUT """
name = input("Nombre?: ")
pincount = input("Pincount?: ")
pincount_ = int(pincount)
create_ecu(name=name, pincount=pincount_)
new_profile(name, pincount_)
def create_ecu(name, pincount):
""" CREATE NEW ECU PROFILE ROUTINE """
if not EcuType.select().where(EcuType.ecu_name == name):
ecu = EcuType.create(ecu_name=name, ecu_pincount=pincount)
ecu.save()
else:
print("ECU NAME ALREADY EXIST")
def new_profile(name, pincount):
""" NEW PROFILE ROUTINE """
ecu = EcuType.select().where(EcuType.ecu_name == name).get()
population = get_profiles(ecu_type=ecu)
sample_data = random.sample(range(1000), pincount)
save_profile(ecu_type=ecu, data=sample_data, ref="0281011900", dbnumber=20211)
results = compliance(new_profiledata=sample_data, known_good_values=population)
if results == COMPLIANT:
print("ECU IS COMPLIANT UNDER STANDARD PROFILING")
else:
for each in results:
print(each)
def compliance(new_profiledata, known_good_values):
"""
Test a new set of ADC values with known good ones
"""
known_and_new = known_good_values[:]
known_and_new.append(new_profiledata)
stdv_list = []
mean_list = []
results = []
arr = numpy.array(known_and_new)
mean_list = numpy.mean(arr, axis=0)
stdv_list = numpy.std(arr, axis=0)
for pin_number, (mean_value, stdv, pin_data) in enumerate(zip(mean_list,
stdv_list,
new_profiledata)):
if (stdv/mean_value) * 100 > STDV_CORRECTION:
results.append(['DEFECT', pin_number, mean_value, stdv, pin_data,
((stdv/mean_value)*100)])
else:
results.append(['OK', pin_number, mean_value, stdv, pin_data,
((stdv/mean_value)*100)])
if not results:
return COMPLIANT
return results
def powerset(seq):
"""
Returns all the subsets of this set. This is a generator.
"""
if len(seq) <= 1:
yield seq
yield []
else:
for item in powerset(seq[1:]):
yield [seq[0]]+item
yield item
def save_profile(ecu_type, data, ref, dbnumber):
""" SAVE PROFILE ROUTINE """
profile_datapacked = msgpack.packb(list(data))
profile = PinData.create(ecu_name=ecu_type,
ecu_ref_number=ref,
ecu_db_number=dbnumber,
pin_reading_msgpack=profile_datapacked)
profile.save()
def get_profiles(ecu_type):
""" GET DB PROFILES """
profile_list = PinData.select().where(PinData.ecu_name == ecu_type)
profile_datalist = []
for each in profile_list:
profile_datalist.append(msgpack.unpackb(each.pin_reading_msgpack))
return profile_datalist
def init_db():
""" STARTUP ROUTINE """
try:
DB.connect()
# logging.DEBUG("Database connection OK")
except PeeweeException as err:
print(err)
# logging.DEBUG("Database connection ERROR")
try:
DB.create_tables([EcuType, PinData])
# logging.DEBUG("Database tables created")
except PeeweeException as err:
print(err)
# logging.CRITICAL("Database tables creation ERROR")