def GetInboundRadioMessageToDB(self): #time.sleep(0.1) for radio in self.radios: if radio.GetIsInitialized(): #print("for each radio initialized") radioMessage = radio.GetRadioData() if radioMessage is not None: print("Radio message received from node: ", end="") print(radioMessage.fromNode) if radioMessage.messageType == PUNCH or radioMessage.messageType == COMBINED_PUNCH: self.PrintRadioMessage(radioMessage) DatabaseHelper.save_radio_message(radioMessage)
def SendAckMessage(self): for radio in self.radios: if radio.GetIsInitialized(): radioNumber = radio.GetRadioNumber() radioMode = self.settings.GetRadioMode(radioNumber) if radioMode == P2P_RETRY: #send simple ack radioMessages = DatabaseHelper.get_last_x_radio_message_data_not_acked(radioNumber, 1) if len(radioMessages) > 0 and radioMessages[0].messageNumber is not None: print("send simple ack") radio.SendSimpleAckMessage(radioMessages[0].messageNumber) radioMessages[0].ackSent = True DatabaseHelper.save_radio_message(radioMessages[0])
def saveRadioSettings(): postedRadioSettings = request.get_json(force=True) #print(postedRadioSettings) radioSettings = RadioSettingsData() #print(radioSettings) radioSettings.id = postedRadioSettings['id'] radioSettings.RadioNumber = postedRadioSettings['RadioNumber'] radioSettings.Description = postedRadioSettings['Description'] radioSettings.ChannelId = postedRadioSettings['ChannelId'] radioSettings.RadioMode = postedRadioSettings['RadioMode'] radioSettings.Enabled = postedRadioSettings['Enabled'] radioSettings.RadioExists = postedRadioSettings['RadioExists'] radioSettings.InboundRadioNodes = [] postedInboundRadioNodes = postedRadioSettings['InboundRadioNodes'] print(postedInboundRadioNodes) for postedNode in postedInboundRadioNodes: node = InboundRadioNodeData() node.id = None node.NodeNumber = postedNode['NodeNumber'] node.RadioSettingsId = None #set later radioSettings.InboundRadioNodes.append(node) savedRadioSettings = DatabaseHelper.save_radio_settings(radioSettings) SettingsClass.SetConfigurationDirty(True) return jsonpickle.encode(MicroMock(RadioSettings=savedRadioSettings)) #@app.route('/getpersonbyid', methods = ['POST']) #def getPersonById(): # personId = int(request.form['personId']) # return str(personId) # back to a string to produce a proper response #personId = request.form.get('personId', type=int)
def GetTimeSlotData(self, radioNumber): nodes = self.settings.GetInboundRadioNodes(radioNumber) timeSlotRecords = [] for node in nodes: timeSlot = TimeSlotData(1, node.NodeNumber, 0) timeSlotRecords.append(timeSlot) # !!! get message only for the radioNumber radioMessages = DatabaseHelper.get_last_x_radio_message_data(MAX_NO_OF_TIMESLOTS_PER_ROUND) for radioMessage in radioMessages: if not radioMessage.ackSent: messageNumberAddedToTimeSlot = False for timeSlotRecord in timeSlotRecords: if timeSlotRecord.nodeNumber == radioMessage.fromNode \ and timeSlotRecord.messageNumber == 0: timeSlotRecord.messageNumber = radioMessage.messageNumber messageNumberAddedToTimeSlot = True if not messageNumberAddedToTimeSlot: timeSlot = TimeSlotData(0, radioMessage.fromNode, radioMessage.messageNumber) timeSlotRecords.append(timeSlot) radioMessage.ackSent = True DatabaseHelper.save_radio_message(radioMessage) return timeSlotRecords
def saveMainSettings(): postedMainSettings = request.get_json(force=True) #print(postedRadioSettings) mainSettings = MainSettingsData() #print(radioSettings) mainSettings.id = None #save_main_settings will always update existing mainSettings.NodeNumber = postedMainSettings['NodeNumber'] mainSettings.SendToMeosDatabase = postedMainSettings['SendToMeosDatabase'] mainSettings.MeosDatabaseServer = postedMainSettings['MeosDatabaseServer'] mainSettings.MeosDatabaseServerPort = postedMainSettings['MeosDatabaseServerPort'] mainSettings.NodeToControlNumberMapping = [] postedNodeToControlNumberMapping = postedMainSettings['NodeToControlNumberMapping'] print(postedNodeToControlNumberMapping) for postedNodeToControl in postedNodeToControlNumberMapping: nodeToCtrl = NodeToControlNumberData() nodeToCtrl.id = None nodeToCtrl.NodeNumber = postedNodeToControl['NodeNumber'] nodeToCtrl.ControlNumber = postedNodeToControl['ControlNumber'] mainSettings.NodeToControlNumberMapping.append(nodeToCtrl) savedMainSettings = DatabaseHelper.save_main_settings(mainSettings) SettingsClass.SetConfigurationDirty(True) return jsonpickle.encode(MicroMock(MainSettings=savedMainSettings))
def SendToCompetitionDatabase(self): punches = DatabaseHelper.get_punches_to_send_to_meos() if self.settings.GetSendToMeosDatabase(): if len(punches) > 0: # Create a TCP/IP socket #time.sleep(1) print("time1") if self.sock is None: try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print("Address: " + self.settings.GetMeosDatabaseServer() + " Port: " + str(self.settings.GetMeosDatabaseServerPort())) server_address = (self.settings.GetMeosDatabaseServer(), self.settings.GetMeosDatabaseServerPort()) self.sock.connect(server_address) print("after connect") except socket.gaierror as msg: print("Address-related error connecting to server: " + str(msg)) print("close") self.sock.close() self.sock = None time.sleep(1) return except socket.error as msg: print("Connection error: " + str(msg)) print("close") self.sock.close() self.sock = None time.sleep(1) return try: for punch in punches: stationNumber = DatabaseHelper.get_control_number_by_node_number(punch.origFromNode) if stationNumber is not None: print("send punch to meos") punchBytesToSend = punch.GetMeosByteArray(stationNumber) # Send data self.sock.sendall(punchBytesToSend) DatabaseHelper.set_punch_sent_to_meos(punch.id) else: DatabaseHelper.set_no_station_number_found(punch.id) except socket.error as msg: print(msg) self.sock = None
def __init__(self): DatabaseHelper.ensure_tables_created() DatabaseHelper.ensure_main_settings_exists() self.settings = SettingsClass() self.timeSlotManager = TimeSlotManager(self.settings) self.nextCallTimeSlotMessage = time.time() self.radios = [] print("init") detectedRadios = self.DetectRadios() radioNumbers = [radio.GetRadioNumber() for radio in detectedRadios] DatabaseHelper.ensure_radio_settings_exists(radioNumbers) self.ConfigureRadios(detectedRadios) if self.IsAnyRadioInP2MRetryMode(): self.SetupSendTimeSlotMessageTimer() battery.SetupBatteryMonitorTimer()
def GetRadioChannel(self, radioNumber): for radioSetting in self.radioSettings: if radioSetting.RadioNumber == radioNumber: channelId = radioSetting.ChannelId return DatabaseHelper.get_channel(channelId) return None
def UpdateFromDatabase(self): self.radioSettings = DatabaseHelper.get_radio_settings_data() self.mainSettings = DatabaseHelper.get_main_settings_data()
def getChannels(): channels = DatabaseHelper.get_channels() return jsonpickle.encode(MicroMock(Channels=channels))
def recreateDatabase(): DatabaseHelper.drop_all_tables() DatabaseHelper.ensure_tables_created() DatabaseHelper.add_default_channels() SettingsClass.SetConfigurationDirty(True) return jsonpickle.encode(MicroMock(message="Database emptied"))
def removeAllPunches(): DatabaseHelper.remove_all_punches() return jsonpickle.encode(MicroMock(message="Punches deleted"))
def getMainSettings(): main = DatabaseHelper.get_main_settings_data() print(main.MeosDatabaseServer) return jsonpickle.encode(MicroMock(mainSettings=main))
def getRadios(): radios = DatabaseHelper.get_radio_settings_data() #for radio in radios: # radio.Title = "Radio " + str(radio.RadioNumber) return jsonpickle.encode(MicroMock(Radios=radios))
def CheckForScanForNewRadiosRequest(self): if self.settings.GetScanForNewRadiosRequest(): SettingsClass.SetScanForNewRadiosRequest(False) detectedRadios = self.DetectRadios() radioNumbers = [radio.GetRadioNumber() for radio in detectedRadios] DatabaseHelper.ensure_radio_settings_exists(radioNumbers)