def remove_subTopics(self, topics): oldSubList = SL('load', 'subscriptions') for i in topics: for g in oldSubList: if (i == g): oldSubList.remove(g) SL('save', 'subscriptions', variables=oldSubList)
def store_history(Data, _raw=True): global maxHistory completeDataset = SL('load', 'Data_history') if (completeDataset == None): completeDataset = {'raw': {}, 'pca': {}} if (_raw): completeDataset['raw'].update( {str(t.asctime(t.localtime())): Data}) else: completeDataset['pca'].update( {str(t.asctime(t.localtime())): Data}) else: if (_raw): n_raw = len(completeDataset['raw']) if (n_raw > maxHistory): completeDataset['raw'].pop( list(completeDataset['raw'].keys())[0]) completeDataset['raw'].update( {str(t.asctime(t.localtime())): Data}) else: n_pca = len(completeDataset['pca']) if (n_pca > maxHistory): completeDataset['pca'].pop( list(completeDataset['pca'].keys())[0]) completeDataset['pca'].update( {str(t.asctime(t.localtime())): Data}) SL('save', 'Data_history', variables=completeDataset)
def add_subTopics(self, newTopics): oldSubList = SL('load', 'subscriptions') for i in newTopics: duple = False for g in oldSubList: if (i == g): duple = True if (not duple): oldSubList.append(i) SL('save', 'subscriptions', variables=oldSubList)
def store_in_pca_inputs(objectRow): global datacount try: objectList = SL('load', 'pcain') except: SL('save', 'pcain', variables=[]) objectList.append(objectRow) SL('save', 'pcain', variables=objectList) datacount += 1 print("DATACOUNT IS " + str(datacount))
def on_message(self, client, userdata, message): #define callback function #print("message received ", str(message.payload.decode("utf-8"))) print("on_message started") global serverClient global localClient global recievedMsg global recentMsg global maxMsgLogs global msgLogIndex msg_topic = message.topic #msg_qos = message.qos; msg_retain = message.retain msg_payload = str(message.payload.decode("utf-8")) if not (msg_retain == 1): recievedMsg = True print("message was not retained") if not (msg_payload == ''): print("on_message: payload not empty") #print(msg_payload); #temp_msg = [msg_payload, msg_topic, msg_qos, msg_retain]; fileName = 'unknown_log' if (client == localClient): fileName = 'local_log' elif (client == serverClient): fileName = 'server_log' print("on_message: filename is " + fileName) partLoad = SL('load', fileName) #Loading up past variables finalMessage = (msg_payload, str(msg_topic), str(t.asctime(t.localtime()))) if (partLoad == None): print("on_message: partLoad was empty") partLoad = [finalMessage] else: print("on_message: partLoad not empty") curLen = len(partLoad) if not (partLoad[-1:][:1] == finalMessage[:1]): print( "on_message: new message not equal to last logged message" ) if not (curLen < maxMsgLogs): print("on_message: message log limit reached") partLoad.remove(partLoad[0]) else: print("on_message: message log limit not reached") partLoad.append(finalMessage) SL('save', fileName, variables=partLoad) # Saving Variables recentMsg = finalMessage else: print("message was retained")
def analyse_pca(): global datacount print("started analysing the pca") PCA_Module.fetchData() PCA_Module.fit_PCA() PCA_Module.display_PCA() store_history(PCA_Module.get_complete_Data(), False) SL('save', 'pcain', variables=[]) datacount = 0
def listen_server(self, seconds): global serverClient subList = SL('load', 'subscriptions') self.init_serverConnection() serverClient.on_connect = self.on_connect serverClient.on_message = self.on_message serverClient.on_publish = self.on_publish serverClient.on_subscribe = self.on_subscribe serverClient.on_log = self.on_log serverClient.publish('default', 'im listening') serverClient.loop_start() serverClient.subscribe(subList) serverClient.publish('default', 'testing1') t.sleep(seconds) serverClient.publish('default', 'testing1') serverClient.loop_stop() serverClient.publish('default', 'im not listening') msg = SL('load', 'server_log')
def listen_until(self): global serverClient global recievedMsg subList = SL('load', 'subscriptions') self.init_serverConnection('88.95.221.115', 'engineer', 'vykgVjYTPDcK') serverClient.on_connect = self.on_connect serverClient.on_message = self.on_message serverClient.on_publish = self.on_publish serverClient.on_subscribe = self.on_subscribe serverClient.on_log = self.on_log serverClient.publish('default', 'im listening') serverClient.loop_start() serverClient.subscribe(subList) while (not recievedMsg): t.sleep(1) serverClient.publish('default', 'testing1') serverClient.loop_stop() serverClient.publish('default', 'im not listening') msg = SL('load', 'server_log') print(msg)
def listen_until(self): global serverClient global recievedMsg global stopListen subList = SL('load', 'subscriptions') self.init_serverConnection() serverClient.on_connect = self.on_connect serverClient.on_message = self.on_message serverClient.on_publish = self.on_publish serverClient.on_subscribe = self.on_subscribe serverClient.on_log = self.on_log serverClient.loop_start() serverClient.subscribe(subList) while (not recievedMsg): if (stopListen): stopListen = False serverClient.loop_stop() return t.sleep(1) t.sleep(5) serverClient.loop_stop() t.sleep(5)
def init_topics(self): SL('save', 'subscriptions', variables=[("default", 0)])
def on_connect(self, mqttc, obj, flags, rc): global serverClient print("obj: " + str(obj)) print("rc: " + str(rc)) subs = SL('load', 'subscriptions') serverClient.subscribe(subs)
def init_data(data): SL('save', 'Data', variables=data)
def analyse_FFT(): global fftReady global q_data while (any(q_data)): fftReady = False prep_data(q_data[0]) q_data = q_data[1:] Data = SL('load', 'Data') delta_avg_time = float(Data['avgdeltatime[us]']) #delta_avg_time = float(Data['avgdeltatime[us]']) x = Data['x[g]'] y = Data['y[g]'] z = Data['z[g]'] v = [] for xyz in zip(x, y, z): v.append( np.sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2])) T = (delta_avg_time * (10**(-6))) Nx = int(len(x)) Ny = int(len(y)) Nz = int(len(z)) Nv = int(len(v)) tx = np.linspace(0.0, Nx * T, Nx) ty = np.linspace(0.0, Ny * T, Ny) tz = np.linspace(0.0, Nz * T, Nz) tv = np.linspace(0.0, Nv * T, Nv) xf = scipy.fftpack.rfft(x) yf = scipy.fftpack.rfft(y) zf = scipy.fftpack.rfft(z) vf = scipy.fftpack.rfft(v) tfx = np.linspace(0.0, 1.0 / (2.0 * T), Nx // 2) tfy = np.linspace(0.0, 1.0 / (2.0 * T), Ny // 2) tfz = np.linspace(0.0, 1.0 / (2.0 * T), Nz // 2) tfv = np.linspace(0.0, 1.0 / (2.0 * T), Nv // 2) fig, ax = plt.subplots() ax.set_title("plotting time series of X axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) ax.plot(tx, x) plt.show() fig, ax = plt.subplots() ax.set_title("plotting time series of Y axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) ax.plot(ty, y) plt.show() fig, ax = plt.subplots() ax.set_title("plotting time series of Z axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) ax.plot(tz, z) plt.show() fig, ax = plt.subplots() ax.set_title("plotting time series of abs vector on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) ax.plot(tv, v) plt.show() fig, ax = plt.subplots() ax.set_title("plotting freq_spectrum of X axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) ax.plot(tfx[1:], 2.0 / Nx * np.abs(xf[1:Nx // 2])) #x_lim = ax.get_ylim() plt.show() fig, ax = plt.subplots() ax.set_title("plotting freq_spectrum of Y axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) ax.plot(tfy[1:], 2.0 / Ny * np.abs(yf[1:Ny // 2])) #y_lim = ax.get_ylim() plt.show() fig, ax = plt.subplots() ax.set_title("plotting freq_spectrum of Z axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) ax.plot(tfz[1:], 2.0 / Nz * np.abs(zf[1:Nz // 2])) #z_lim = ax.get_ylim() plt.show() fig, ax = plt.subplots() ax.set_title("plotting freq_spectrum of abs vector on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) ax.plot(tfv[1:], 2.0 / Nv * np.abs(vf[1:Nv // 2])) #v_lim = ax.get_ylim() plt.show() # xf_avg = avrg_list(xf[1:]) # yf_avg = avrg_list(yf[1:]) # zf_avg = avrg_list(zf[1:]) # vf_avg = avrg_list(vf[1:]) #fig, ax = plt.subplots() # ax.set_title("plotting averaged freq_spectrum of X axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) # ax.plot(tfx[1:], 2.0/Nx * np.abs(xf_avg[0][1:Nx//2])) # ax.set_ylim(x_lim) # plt.show() # fig, ax = plt.subplots() # ax.set_title("plotting averaged freq_spectrum of Y axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) # ax.plot(tfy[1:], 2.0/Ny * np.abs(yf_avg[0][1:Ny//2])) # ax.set_ylim(y_lim) # plt.show() # fig, ax = plt.subplots() # ax.set_title("plotting averaged freq_spectrum of Z axis on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) # ax.plot(tfz[1:], 2.0/Nz * np.abs(zf_avg[0][1:Nz//2])) # ax.set_ylim(z_lim) # plt.show() # fig, ax = plt.subplots() # ax.set_title("plotting averaged freq_spectrum of abs vector on engine: " + Data['tag'] + " recieved " + Data['timerecieved']) # ax.plot(tfv[1:], 2.0/Nv * np.abs(vf_avg[0][1:Nv//2])) # ax.set_ylim(v_lim) # plt.show() time_recieved = Data['timerecieved'] tag = Data['tag'] temperaturedifference = float(Data['temperaturedifference']) rpm = float(Data['rpm']) _objectRow = [ tag, time_recieved, temperaturedifference, rpm, *yf[:len(yf) // 2] ] store_in_pca_inputs(_objectRow)
import threading import numpy as np import matplotlib.pyplot as plt import scipy.fftpack from kb_utils import SaveLoad as SL listening = True # TODO Need to add function q_data = [] threads = [] sensorListener = mqttInterface.mqtt_User() lastMsg = "" timesRetry = 0 maxTimesRetry = 3 fftReady = False maxHistory = 50 datacount = len(SL('load', 'pcain')) def init_data(data): SL('save', 'Data', variables=data) def prep_data(Data): data_dir = Data[2].split('"') columns = [] value = [] key = False for piece in data_dir: next_ = True if not (len(piece) < 2): piece = piece.lower()
def get_msgLog(self): msgLog = SL('load', 'server_log') return msgLog
# -*- coding: utf-8 -*- """ Created on Fri Apr 3 16:54:50 2020 @author: Remi """ import matplotlib.pyplot as plt #import pandas as pd import numpy as np from kb_utils import SaveLoad as SL config_file = SL('load', 'Data') #Load datafile with Kb_utils print(config_file) #x_files = config_file['1'][0]['x_files'] #EngineId, timestamp, Column name array_sine = [] sine_t = np.linspace(0, 0.001, 100) for t in sine_t: array_sine.append(np.sin(t)) #plt.ylabel("Amplitude") #plt.xlabel("Frequency [Hz]") plt.plot(sine_t, array_sine) plt.grid() plt.show() # fig = plt.figure() # ax = fig.add_subplot(1,1,1)
def init_messageLogs(self): SL('save', 'server_log') SL('save', 'local_log') SL('save', 'unknown_log')