def run(self, id): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.sock.settimeout(1.0) self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, struct.pack('b', 1)) message = "" result = None try: if len(self.messages) > 0: # Send data to the multicast group message = self.messages.pop() self.sock.sendto(message, (self.MCAST_ADDR, self.MCAST_PORT)) # Look for responses from all recipients result = [] while True: try: if select.select([self.sock], [], [], 3)[0]: # wait 5 seconds try: data, server = self.sock.recvfrom(8192) result.append({'data': data, 'from': server, 'name': socket.gethostbyaddr(server[0])[0]}) MonitorLog.addLog(int(data.split('|')[0]), 1, 'income', data.split('|')[1]) signal.send('monitorserver', 'clientanswer', clientid=data.split('|')[0], value=data.split('|')[1:], ip=server, name=socket.gethostbyaddr(server[0])[0]) except socket.timeout: break except: pass else: # timeout reached break except socket.error: break finally: self.sock.close() self.results[id] = (message, result)
def loadHousenumbersFromOsm(streets): # load all housenumbers from osm """ Load all housenumbers of given street from OpenStreetMap :param streets: list of :py:class:`emonitor.modules.streets.street.Street` :return: number of housenumbers of given streeet """ global URL housenumbers = {} for street in streets: nodes = {} ways = [] city = City.getCities(id=street.cityid) SEARCHSTRING = 'area[name~"%s"];way(area)[building]["addr:street"="%s"];(._;>;);out;' % ( city.osmname, street.name) r = requests.post(URL, data={'data': SEARCHSTRING}) xmldoc = minidom.parseString(r.content) for node in xmldoc.getElementsByTagName('node'): nodes[node.attributes['id'].value] = [ float(node.attributes['lat'].value), float(node.attributes['lon'].value) ] for way in xmldoc.getElementsByTagName('way'): ways.append(way) # try with associatedStreet SEARCHSTRING = 'area[name~"%s"];rel[type=associatedStreet](area)->.allASRelations;way(r.allASRelations:"street")[name="%s"];rel(bw:"street")[type=associatedStreet]->.relationsWithRoleStreet;way(r.relationsWithRoleStreet)[building];(._;>;);out;' % ( city.osmname, street.name) r = requests.post(URL, data={'data': SEARCHSTRING}) xmldoc = minidom.parseString(r.content) for node in xmldoc.getElementsByTagName('node'): nodes[node.attributes['id'].value] = [ float(node.attributes['lat'].value), float(node.attributes['lon'].value) ] for way in xmldoc.getElementsByTagName('way'): ways.append(way) _numbers = 0 for way in ways: nd = [] _id = None for c in way.childNodes: if c.nodeName == "nd": nd.append(nodes[c.attributes['ref'].value]) if c.nodeName == "tag" and c.attributes[ 'k'].value == 'addr:housenumber': _id = c.attributes['v'].value if _id: housenumbers[_id] = nd street.addHouseNumber(_id, housenumbers[_id]) _numbers += 1 signal.send('housenumber', 'osm', street=street.name, hnumbers=_numbers, position=(streets.index(street) + 1, len(streets)), cityid=city.id) signal.send('housenumber', 'osmdone', numbers=len(housenumbers)) return len(housenumbers)
def loadTiles(path, tilelist): """ Load map tiles into path from given tilelist :param path: path to store map tiles :param tilelist: list of tiles to load from OSM :return: progress information *[position, number of tiles to load]* as list """ from emonitor.extensions import scheduler LOADTILES.append(tilelist[min(tilelist.keys())][0]) # add selected tile CURRENTLOADING.extend(sum(tilelist.values(), [])) # add tiles signal.send('map', 'tiledownloadstart', tiles=tilelist[min(tilelist.keys())][0]) def doLoadTiles(**kwargs): def getTile(zoom, item): response = urllib2.urlopen('http://a.tile.openstreetmap.org/{}/{}/{}.png'.format(zoom, item[0], item[1])) with open('{}/{}/{}-{}.png'.format(path, zoom, item[0], item[1]), 'wb') as fout: fout.write(response.read()) if item in CURRENTLOADING: CURRENTLOADING.remove(item) if 'path' in kwargs: path = kwargs['path'] else: return if 'tilelist' in kwargs: tilelist = kwargs['tilelist'] else: return errortiles = [] for zoom in tilelist: if not os.path.exists('{}/{}'.format(path, zoom)): os.makedirs('{}/{}'.format(path, zoom)) for item in tilelist[zoom]: if len(CURRENTLOADING) == 0: # loding stopped or ended return if (len(LOADTILES) * 5460 - len(CURRENTLOADING)) % 10 == 0: # send state every 10 loads signal.send('map', 'tiledownloadprogress', progress=(len(LOADTILES) * 5460 - len(CURRENTLOADING), len(LOADTILES) * 5460), tiles=LOADTILES) try: getTile(zoom, item) except: errortiles.append((zoom, item)) # try error objects for err in errortiles: try: getTile(err[0], err[1]) except: print "error in {}".format(err) signal.send('map', 'tiledownloaddone', tiles=tilelist[min(tilelist.keys())][0]) scheduler.add_job(doLoadTiles, kwargs={'path': path, 'tilelist': tilelist}) return 1 # loading started
def run(self, id): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.sock.settimeout(1.0) self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, struct.pack('b', 1)) message = "" result = None try: if len(self.messages) > 0: # Send data to the multicast group message = self.messages.pop() self.sock.sendto(message, (self.MCAST_ADDR, self.MCAST_PORT)) # Look for responses from all recipients result = [] while True: try: if select.select([self.sock], [], [], 3)[0]: # wait 5 seconds try: data, server = self.sock.recvfrom(8192) result.append({ 'data': data, 'from': server, 'name': socket.gethostbyaddr(server[0])[0] }) MonitorLog.addLog(int(data.split('|')[0]), 1, 'income', data.split('|')[1]) signal.send('monitorserver', 'clientanswer', clientid=data.split('|')[0], value=data.split('|')[1:], ip=server, name=socket.gethostbyaddr( server[0])[0]) except socket.timeout: break except: pass else: # timeout reached break except socket.error: break finally: self.sock.close() self.results[id] = (message, result)
def doLoadTiles(**kwargs): def getTile(zoom, item): response = urllib2.urlopen( 'http://a.tile.openstreetmap.org/{}/{}/{}.png'.format( zoom, item[0], item[1])) with open('{}/{}/{}-{}.png'.format(path, zoom, item[0], item[1]), 'wb') as fout: fout.write(response.read()) if item in CURRENTLOADING: CURRENTLOADING.remove(item) if 'path' in kwargs: path = kwargs['path'] else: return if 'tilelist' in kwargs: tilelist = kwargs['tilelist'] else: return errortiles = [] for zoom in tilelist: if not os.path.exists('{}/{}'.format(path, zoom)): os.makedirs('{}/{}'.format(path, zoom)) for item in tilelist[zoom]: if len(CURRENTLOADING) == 0: # loding stopped or ended return if (len(LOADTILES) * 5460 - len(CURRENTLOADING) ) % 10 == 0: # send state every 10 loads signal.send( 'map', 'tiledownloadprogress', progress=(len(LOADTILES) * 5460 - len(CURRENTLOADING), len(LOADTILES) * 5460), tiles=LOADTILES) try: getTile(zoom, item) except: errortiles.append((zoom, item)) # try error objects for err in errortiles: try: getTile(err[0], err[1]) except: print "error in {}".format(err) signal.send('map', 'tiledownloaddone', tiles=tilelist[min(tilelist.keys())][0])
def loadHousenumbersFromOsm(streets): # load all housenumbers from osm """ Load all housenumbers of given street from OpenStreetMap :param streets: list of :py:class:`emonitor.modules.streets.street.Street` :return: number of housenumbers of given streeet """ global URL housenumbers = {} for street in streets: nodes = {} ways = [] city = City.getCities(id=street.cityid) SEARCHSTRING = 'area[name~"%s"];way(area)[building]["addr:street"="%s"];(._;>;);out;' % (city.osmname, street.name) r = requests.post(URL, data={'data': SEARCHSTRING}) xmldoc = minidom.parseString(r.content) for node in xmldoc.getElementsByTagName('node'): nodes[node.attributes['id'].value] = [float(node.attributes['lat'].value), float(node.attributes['lon'].value)] for way in xmldoc.getElementsByTagName('way'): ways.append(way) # try with associatedStreet SEARCHSTRING = 'area[name~"%s"];rel[type=associatedStreet](area)->.allASRelations;way(r.allASRelations:"street")[name="%s"];rel(bw:"street")[type=associatedStreet]->.relationsWithRoleStreet;way(r.relationsWithRoleStreet)[building];(._;>;);out;' % (city.osmname, street.name) r = requests.post(URL, data={'data': SEARCHSTRING}) xmldoc = minidom.parseString(r.content) for node in xmldoc.getElementsByTagName('node'): nodes[node.attributes['id'].value] = [float(node.attributes['lat'].value), float(node.attributes['lon'].value)] for way in xmldoc.getElementsByTagName('way'): ways.append(way) _numbers = 0 for way in ways: nd = [] _id = None for c in way.childNodes: if c.nodeName == "nd": nd.append(nodes[c.attributes['ref'].value]) if c.nodeName == "tag" and c.attributes['k'].value == 'addr:housenumber': _id = c.attributes['v'].value if _id: housenumbers[_id] = nd street.addHouseNumber(_id, housenumbers[_id]) _numbers += 1 signal.send('housenumber', 'osm', street=street.name, hnumbers=_numbers, position=(streets.index(street) + 1, len(streets)), cityid=city.id) signal.send('housenumber', 'osmdone', numbers=len(housenumbers)) return len(housenumbers)
def getClients(self): signal.send('monitorserver', 'clientsearchstart', clients=[]) monitors = Monitor.getMonitors() message, results = self.sendMessageWithReturn('0', 'ping') clients = {} for res in results: _id = res['data'].split('|')[0] clients[_id] = [(res['from'][0], res['name'])] if _id not in [str(m.id) for m in monitors]: clients[_id].append(None) for monitor in monitors: if str(monitor.id) in clients.keys(): clients[str(monitor.id)].append(monitor) else: clients[str(monitor.id)] = [None, monitor] MonitorServer.clients = {'clients': clients, 'time': datetime.datetime.now()} signal.send('monitorserver', 'clientsearchdone', clients=clients.keys()) return clients
def getClients(self): """ client search method collects answer of each client and delivers list. client answer syntax: | separated string - clientid|state|parameters """ signal.send('monitorserver', 'clientsearchstart', clients=[]) monitors = Monitor.getMonitors() message, results = self.sendMessageWithReturn('0', 'ping') clients = {} for res in results: _state = _id = "" _data = res['data'].split('|') print "data:", _data if _data[0]: _id = _data[0] if _data[1]: _state = _data[1] if len(_data) > 2: _params = _data[2:] else: _params = [] if _state == 'initneed': self.sendMessage(_id, 'load') clients[_id] = [(res['from'][0], res['name'], _params)] if _id not in [str(m.id) for m in monitors]: clients[_id].append(None) for monitor in monitors: if str(monitor.id) in clients.keys(): clients[str(monitor.id)].append(monitor) else: clients[str(monitor.id)] = [None, monitor] MonitorServer.clients = { 'clients': clients, 'time': datetime.datetime.now() } signal.send('monitorserver', 'clientsearchdone', clients=clients.keys()) return clients
def doLoadTiles(**kwargs): def getTile(zoom, item): response = urllib2.urlopen('http://a.tile.openstreetmap.org/{}/{}/{}.png'.format(zoom, item[0], item[1])) with open('{}/{}/{}-{}.png'.format(path, zoom, item[0], item[1]), 'wb') as fout: fout.write(response.read()) if item in CURRENTLOADING: CURRENTLOADING.remove(item) if 'path' in kwargs: path = kwargs['path'] else: return if 'tilelist' in kwargs: tilelist = kwargs['tilelist'] else: return errortiles = [] for zoom in tilelist: if not os.path.exists('{}/{}'.format(path, zoom)): os.makedirs('{}/{}'.format(path, zoom)) for item in tilelist[zoom]: if len(CURRENTLOADING) == 0: # loding stopped or ended return if (len(LOADTILES) * 5460 - len(CURRENTLOADING)) % 10 == 0: # send state every 10 loads signal.send('map', 'tiledownloadprogress', progress=(len(LOADTILES) * 5460 - len(CURRENTLOADING), len(LOADTILES) * 5460), tiles=LOADTILES) try: getTile(zoom, item) except: errortiles.append((zoom, item)) # try error objects for err in errortiles: try: getTile(err[0], err[1]) except: print "error in {}".format(err) signal.send('map', 'tiledownloaddone', tiles=tilelist[min(tilelist.keys())][0])
def processFile(incomepath, filename): """ run processing in test mode """ params = dict(incomepath=incomepath, filename=filename, mode='test') handlers = events.getEvents('file_added').getHandlerList() dbhandlers = Eventhandler.getEventhandlers(event='file_added') for handler in dbhandlers: # db for hdl in handlers: if handler.handler == hdl[0]: #p = hdl[1]('file_added', params) params.update(hdl[1]('file_added', **params)) res = [] for p in handler.getParameterList(): try: res.append(u'{}:{}'.format(p, params[p.split('.')[1]])) except: try: if p.split(u'.')[1] in params.keys(): res = [u'{}:{}'.format(p, params[p.split('.')[1]])] else: res = [u'error: key not found - {}'.format(p.split('.')[1])] params['error'] = u'error: key not found - {}'.format(p.split('.')[1]) except: import traceback print traceback.format_exc() if u'error' in params.keys(): signal.send('alarm', 'testupload_start', result=res, handler=handler.handler.split('.')[-1], protocol=params['time'][-1], error=params['error']) else: signal.send('alarm', 'testupload_start', result=res, handler=handler.handler.split('.')[-1], protocol=params['time'][-1]) signal.send('alarm', 'testupload_start', result='done') return params
def getClients(self): """ client search method collects answer of each client and delivers list. client answer syntax: | separated string - clientid|state|parameters """ signal.send('monitorserver', 'clientsearchstart', clients=[]) monitors = Monitor.getMonitors() message, results = self.sendMessageWithReturn('0', 'ping') clients = {} for res in results: _state = _id = "" _data = res['data'].split('|') print "data:", _data if _data[0]: _id = _data[0] if _data[1]: _state = _data[1] if len(_data) > 2: _params = _data[2:] else: _params = [] if _state == 'initneed': self.sendMessage(_id, 'load') clients[_id] = [(res['from'][0], res['name'], _params)] if _id not in [str(m.id) for m in monitors]: clients[_id].append(None) for monitor in monitors: if str(monitor.id) in clients.keys(): clients[str(monitor.id)].append(monitor) else: clients[str(monitor.id)] = [None, monitor] MonitorServer.clients = {'clients': clients, 'time': datetime.datetime.now()} signal.send('monitorserver', 'clientsearchdone', clients=clients.keys()) return clients
def processFile(incomepath, filename): """ run processing in test mode """ params = dict(incomepath=incomepath, filename=filename, mode='test') handlers = events.getEvents('file_added').getHandlerList() dbhandlers = Eventhandler.getEventhandlers(event='file_added') from emonitor.extensions import db db.session.rollback() for handler in dbhandlers: # db for hdl in handlers: if handler.handler == hdl[0]: res = [] try: params.update(hdl[1]('file_added', **params)) except: params.update({'error': u'<b>error in handler {}:</b><br/> <em>{}</em><br>'.format(hdl[0], traceback.format_exc().splitlines())}) for p in handler.getParameterList(): try: res.append(u'{}:\n{}'.format(p, params[p.split('.')[1]].strip())) except: try: if p.split(u'.')[1] in params.keys(): res = [u'{}:\n{}\n'.format(p, params[p.split('.')[1]].strip())] else: with babel.app.test_request_context('/') as ctx: ctx.push() res = [u'<b style="color:red">{}</b><br>'.format(babel.gettext('alarm.testcontext.parametererror').format(p))] params['error'] = babel.gettext('alarm.handler.missing.parameter').format(p, babel.gettext(handler.handler)) ctx.pop() except: signal.send('alarm', 'testupload_start', result='done') res = "\n".join(res) if u'error' in params.keys(): signal.send('alarm', 'testupload_start', result=res, handler=handler.handler.split('.')[-1], protocol=params['time'][-1], error=params['error']) else: signal.send('alarm', 'testupload_start', result=res, handler=handler.handler.split('.')[-1], protocol=params['time'][-1]) signal.send('alarm', 'testupload_start', result='done') return params
def processFile(incomepath, filename): """ run processing in test mode """ params = dict(incomepath=incomepath, filename=filename, mode='test') handlers = events.getEvents('file_added').getHandlerList() dbhandlers = Eventhandler.getEventhandlers(event='file_added') from emonitor.extensions import db db.session.rollback() for handler in dbhandlers: # db for hdl in handlers: if handler.handler == hdl[0]: res = [] try: params.update(hdl[1]('file_added', **params)) except: params.update({'error': u'<b>error in handler {}:</b><br/> <em>{}</em><br>'.format(hdl[0], traceback.format_exc().splitlines())}) for p in handler.getParameterList(): try: res.append(u'{}:{}'.format(p, params[p.split('.')[1]])) except: try: if p.split(u'.')[1] in params.keys(): res = [u'{}:{}'.format(p, params[p.split('.')[1]])] else: res = [u'<b>error: key not found:</b> <em>{}</em><br>'.format(p.split('.')[1])] if not params.get('error'): params['error'] = "" params['error'] += u'<b>error: key not found</b> <em>{}</em><br>'.format(p.split('.')[1]) except: signal.send('alarm', 'testupload_start', result='done') if u'error' in params.keys(): signal.send('alarm', 'testupload_start', result=res, handler=handler.handler.split('.')[-1], protocol=params['time'][-1], error=params['error']) else: signal.send('alarm', 'testupload_start', result=res, handler=handler.handler.split('.')[-1], protocol=params['time'][-1]) signal.send('alarm', 'testupload_start', result='done') return params
def getAdminData(self, **params): """ Deliver admin content of module maps (ajax) :return: rendered template as string or json dict """ if request.args.get('action') == 'checkpath': if os.path.exists(request.args.get('path')): return '1' return '0' elif request.args.get('action') == 'loadmap': # load tiles tile_path = request.args.get('path') def calcSubItems((pos)): return [(2 * int(pos[0]), 2 * int(pos[1])), (2 * int(pos[0]) + 1, 2 * int(pos[1])), (2 * int(pos[0]), 2 * int(pos[1]) + 1), (2 * int(pos[0]) + 1, 2 * int(pos[1]) + 1)] #def getTile(zoom, pos): # global tile_path # response = urllib2.urlopen('http://a.tile.openstreetmap.org/%s/%s/%s.png' % (zoom, pos[0], pos[1])) # #fout = open(tile_path + str(zoom)+'/'+str(pos[0])+'-'+str(pos[1])+'.png', 'wb') # fout = open("%s%s/%s-%s.png" % (tile_path, zoom, pos[0], pos[1]), 'wb') # fout.write(response.read()) # fout.close() _items = {12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: []} for t in request.args.get('tiles').split("-"): if t != '': _items[12].append(t.split(",")) for zoom in range(13, 19): for i in _items[zoom - 1]: _items[zoom] += calcSubItems(i) result = map_utils.loadTiles( '%s%s' % (current_app.config.get('PATH_TILES'), request.args.get('path')), _items) if result == 0: return babel.gettext( 'settings.map.loadinginprogress') # loading still active elif result == 1: return babel.gettext( 'settings.map.loadingstarted') # loading started return "" elif request.args.get('action') == 'stoptileload': # stop tile download for i in map_utils.LOADTILES: signal.send('map', 'tiledownloaddone', tiles=i) map_utils.LOADTILES = [] map_utils.CURRENTLOADING = [] return {} elif request.args.get('action') == 'maptiles': _map = Map.getMaps(id=request.args.get('id')) if _map: return Map.getMapBox(tilepath=current_app.config.get('PATH_TILES'), mappath=_map.path) return Map.getMapBox() elif request.args.get( 'action') == 'loadosmdata': # load all data from openstreetmap from emonitor.extensions import scheduler from emonitor.lib.osm.loaddata import parseOsmData #import time, datetime mapdata = Map.getMaps()[0].getMapBox( tilepath=current_app.config.get('PATH_TILES')) lat = [mapdata['min_latdeg']] while lat[-1] + .05 < mapdata['max_latdeg']: lat.append(lat[-1] + .05) lat.append(mapdata['max_latdeg']) lng = [mapdata['min_lngdeg']] while lng[-1] + .05 < mapdata['max_lngdeg']: lng.append(lng[-1] + .05) lng.append(mapdata['max_lngdeg']) scheduler.add_job(parseOsmData, kwargs={ 'lat': lat, 'lng': lng, 'path': current_app.config.get('PATH_DATA') }) return {'job': 'started'} elif request.args.get( 'action') == 'findcity': # search citystring and deliver position return map_utils.loadPositionOfCity(request.args.get('cityname')) return ""
def myDone(event): if event.job.id: signal.send('scheduler', 'process', jid=event.job.id, state='done')
def handleEvent(eventname, **kwargs): """ Eventhandler for alarm class :param eventname: name of event :param kwargs: parameter list: error, fields, filename, id, incomepath, mode, time :return: all kwargs """ from emonitor import app global LASTALARM alarm_fields = dict() stime = time.time() alarmtype = None for t in AlarmType.getAlarmTypes(): #if re.search(t.keywords.replace('\r\n', '|'), unicode(kwargs['text'], errors='ignore')): if re.search(t.keywords.replace('\r\n', '|'), kwargs['text']): alarm_fields = t.interpreterclass().buildAlarmFromText(t, kwargs['text']) if u'error' in alarm_fields.keys(): kwargs['error'] = alarm_fields['error'] del alarm_fields['error'] alarmtype = t break # copy file -> original name if 'time' in alarm_fields and alarm_fields['time'][1] == 1: # found correct time t = datetime.datetime.strptime(alarm_fields['time'][0], '%d.%m.%Y - %H:%M:%S') else: t = datetime.datetime.now() if not os.path.exists('{}{}'.format(app.config.get('PATH_DONE'), t.strftime('%Y/%m/'))): os.makedirs('{}{}'.format(app.config.get('PATH_DONE'), t.strftime('%Y/%m/'))) try: shutil.copy2('{}{}'.format(kwargs['incomepath'], kwargs['filename']), '{}{}{}'.format(app.config.get('PATH_DONE'), t.strftime('%Y/%m/%Y%m%d-%H%M%S'), os.path.splitext(kwargs['filename'])[1])) except: pass try: # remove file os.remove('{}{}'.format(kwargs['incomepath'], kwargs['filename'])) except: pass kwargs['filename'] = '{}{}'.format(t.strftime('%Y/%m/%Y%m%d-%H%M%S'), os.path.splitext(kwargs['filename'])[1]) logger.debug('alarm_fields: {}'.format(alarm_fields)) if len(alarm_fields) == 0: # no alarmfields found kwargs['id'] = 0 logger.error('no alarm fields found.') return kwargs kwargs['fields'] = '' for k in alarm_fields: kwargs['fields'] = u'{}\n-{}:\n {}'.format(kwargs['fields'], k, alarm_fields[k]) if not alarmtype: # alarmtype not found kwargs['id'] = 0 kwargs['error'] = 'alarmtype not found' logger.error('alarmtype not found.') return kwargs # position _position = dict(lat=u'0.0', lng=u'0.0') if USE_NOMINATIM == 1: try: url = 'http://nominatim.openstreetmap.org/search' params = 'format=json&city={}&street={}'.format(alarm_fields['city'][0], alarm_fields['address'][0]) if 'streetno' in alarm_fields: params += ' {}'.format(alarm_fields['streetno'][0].split()[0]) # only first value r = requests.get('{}?{}'.format(url, params)) _position = dict(lat=r.json()[0]['lat'], lng=r.json()[0]['lon']) except: pass # create alarm object if 'key' not in alarm_fields.keys() or alarm_fields['key'][0] == u'': if alarmtype.translation(u'_bma_main_') in alarm_fields['remark'][0] or alarmtype.translation(u'_bma_main_') in alarm_fields['person'][0]: alarmkey = Alarmkey.query.filter(Alarmkey.key.like(u"%{}%".format(alarmtype.translation(u'_bma_')))).all() if len(alarmkey) > 0: alarm_fields['key'] = ('{}: {}'.format(alarmkey[0].category, alarmkey[0].key), str(alarmkey[0].id)) else: alarm_fields['key'] = (alarmtype.translation(u'_bma_key_'), u'0') if 'time' in alarm_fields and alarm_fields['time'][1] == 1: # found correct time t = datetime.datetime.strptime(alarm_fields['time'][0], '%d.%m.%Y - %H:%M:%S') else: t = datetime.datetime.now() alarm = Alarm(t, alarm_fields['key'][0], 1, 0) alarm.set('id.key', alarm_fields['key'][1]) alarm.material = dict(cars1='', cars2='', material='') # set required attributes alarm.set('marker', '0') alarm.set('filename', kwargs['filename']) alarm.set('priority', '1') # set normal priority alarm.set('alarmtype', alarmtype.name) # set checker name alarm.state = 1 # city if 'city' in alarm_fields and alarm_fields['city'][1] != 0: alarm.city = City.getCities(id=alarm_fields['city'][1]) if alarm_fields['address'][1] != 0: alarm.street = Street.getStreets(id=alarm_fields['address'][1]) else: # city not found -> build from fax url = 'http://nominatim.openstreetmap.org/search' params = u'format=json&city={}&street={}'.format(alarm_fields['city'][0].split()[0], alarm_fields['address'][0]) if 'streetno' in alarm_fields and alarm_fields['streetno'][0]: params += u' {}'.format(alarm_fields['streetno'][0].split()[0]) # only first value alarm.set('streetno', alarm_fields['streetno'][0]) r = requests.get(u'{}?{}'.format(url, params)) logger.debug('load address data from nomination with parameters: city=%s street=%s' % (alarm_fields['city'][0].split()[0], alarm_fields['address'][0])) try: _position = dict(lat=r.json()[0]['lat'], lng=r.json()[0]['lon']) alarm.position = _position except: pass alarm.set('city', alarm_fields['city'][0].split()[0]) alarm.set('id.city', alarm_fields['city'][1]) alarm.set('address', alarm_fields['address'][0]) if alarm_fields['address'][1] != 0: alarm.street = Street.getStreets(id=alarm_fields['address'][1]) if 'cars' in alarm_fields: # add cars found in material for _c in alarm_fields['cars'][1].split(';'): alarm.set('k.cars1', alarm.get('k.cars1') + ';' + _c) # street / street2 if 'address' in alarm_fields and alarm_fields['address'][0] != '': # check correct city -> change if street has different city if len(str(alarm_fields['address'][1]).split(';')) > 0 and alarm_fields['address'][1] != 0: _c = [] for s in str(alarm_fields['address'][1]).split(';'): _s = Street.getStreets(id=s) if _s.cityid and _s.cityid not in _c and _s.cityid == alarm_fields['city'][1]: _c.append(_s.cityid) alarm.street = _s if 'object' in alarm_fields and str(alarm_fields['object'][1]) == '0': if 'lat' not in alarm_fields and 'lng' not in alarm_fields: alarm.position = dict(lat=_s.lat, lng=_s.lng, zoom=_s.zoom) if _position['lat'] != u'0.0' and _position['lng'] != u'0.0': # set marker if nominatim delivers result alarm.position = _position alarm.set('marker', '1') else: # add unknown street alarm.set('id.address', 0) alarm.set('address', alarm_fields['address'][0]) # houseno if 'streetno' in alarm_fields.keys(): alarm.set('streetno', alarm_fields['streetno'][0]) if 'id.streetno' in alarm_fields and 'lat' in alarm_fields and 'lng' in alarm_fields: alarm.position = dict(lat=alarm_fields['lat'][0], lng=alarm_fields['lng'][0]) alarm.set('id.streetno', alarm_fields['id.streetno'][1]) else: # new hn = alarm.street.getHouseNumber(name=alarm_fields['streetno'][0]) if hn: alarm.position = hn.getPosition(0) if 'zoom' in alarm_fields.keys(): alarm.set('zoom', alarm_fields['zoom'][0]) # crossing if 'crossing' in alarm_fields and alarm_fields['crossing'][0] != '': if 'crossing' in alarm_fields and alarm_fields['address'][1] != alarm_fields['crossing'][1]: alarm.set('id.address2', alarm_fields['crossing'][1]) alarm.set('address2', alarm_fields['crossing'][0]) else: alarm.set('id.address2', '0') alarm.set('address2', alarm_fields['crossing'][0]) # addresspart if 'addresspart' in alarm_fields and alarm_fields['addresspart'][0] != '' and alarm_fields['addresspart'][0] != alarm_fields['address'][0]: if alarm_fields['addresspart'][1] > 0: if len(str(alarm_fields['addresspart'][1]).split(';')) > 0: _c = [] for s in str(alarm_fields['addresspart'][1]).split(';'): try: _s = Street.getStreets(id=s) if _s.cityid not in _c and _s.cityid == alarm_fields['city'][1]: _c.append(_s.cityid) alarm.set('id.address2', _s.id) except: pass else: alarm.set('id.address2', alarm_fields['addresspart'][1]) else: alarm.set('id.address2', '0') alarm.set('address2', alarm_fields['addresspart'][0]) # person if 'person' in alarm_fields and alarm_fields['person'][0] != '': alarm.set('person', alarm_fields['person'][0]) # alarmplan if 'alarmplan' in alarm_fields and alarm_fields['alarmplan'][0] != '': alarm.set('alarmplan', alarm_fields['alarmplan'][0]) # alarmobject _ao = None if 'object' in alarm_fields and alarm_fields['object'][0] != '' and 'city' in alarm_fields and alarm_fields['city'][1] > 0: alarm.set('object', alarm_fields['object'][0]) alarm.set('id.object', alarm_fields['object'][1]) # alarmplan from object if alarm_fields['object'][1] != 0: _ao = AlarmObject.getAlarmObjects(id=alarm_fields['object'][1]) if _ao: if _ao.alarmplan != 0: alarm.set('alarmplan', _ao.alarmplan) if _ao.street.id != alarm_fields['address'][1]: # street config from alarmobject alarm.street = Street.getStreets(id=_ao.street.id) if _ao.streetno == "": alarm.set('streetno', alarm_fields['streetno'][0]) else: alarm.set('streetno', _ao.streetno) alarm.position = dict(lat=_ao.lat, lng=_ao.lng, zoom=_ao.zoom) # remark if 'remark' in alarm_fields and alarm_fields['remark'][0] != '': alarm.set('remark', alarm_fields['remark'][0]) if alarmtype.translation(u'_bma_main_') in alarm_fields['remark'][0] or alarmtype.translation(u'_bma_main_') in alarm_fields['person'][0]: alarmkey = Alarmkey.query.filter(Alarmkey.key.like(u"%{}%".format(alarmtype.translation(u'_bma_')))).first() if alarmkey: alarm.set('id.key', alarmkey.id) alarm._key = u'{}: {}'.format(alarmkey.category, alarmkey.key) else: alarm.set('id.key', '0') alarm._key = alarmtype.translation(u'_bma_key_') # additional remarks if 'remark2' in alarm_fields and alarm_fields['remark2'][0] != '': alarm.set('remark', u'%s\n%s' % (alarm.get('remark'), alarm_fields['remark2'][0])) # material if alarm.get('id.key') != 0 and 'city' in alarm_fields: # found key with aao if alarm_fields['city'][1] != 0: # default city if Department.getDepartments(id=alarm.city.dept).defaultcity == alarm_fields['city'][1]: # default city for dep if 'material' in alarm_fields: if str(alarm_fields['material'][1])[0] == '0': # default cars for aao try: alarm.material = dict(cars1=u','.join([str(c.id) for c in alarm.key.getCars1(alarm.street.city.dept)]), cars2=u','.join([str(c.id) for c in alarm.key.getCars2(alarm.street.city.dept)]), material=u','.join([str(c.id) for c in alarm.key.getMaterial(alarm.street.city.dept)])) except AttributeError: alarm.material = dict(cars1=u','.join([str(c.id) for c in alarm.key.getCars1(alarm.city.dept)]), cars2=u','.join([str(c.id) for c in alarm.key.getCars2(alarm.city.dept)]), material=u','.join([str(c.id) for c in alarm.key.getMaterial(alarm.city.dept)])) for _c in u'{}'.format(alarm_fields['material'][1]).split(','): # add additional cars if _c != '0' and _c not in alarm.get('k.cars1').split(','): alarm.set('k.cars1', u'{},{}'.format(alarm.get('k.cars1'), _c)) else: # only alarmed material alarm.material = dict(cars1=alarm_fields['material'][1]) else: # else city if alarm_fields['material'][1] == u'0': # default cars for aao alarm.material = dict(cars1=u','.join([str(c.id) for c in alarm.key.getCars1(alarm.city.dept)]), cars2=u','.join([str(c.id) for c in alarm.key.getCars2(alarm.city.dept)]), material=u','.join([str(c.id) for c in alarm.key.getMaterial(alarm.city.dept)])) else: alarm.material = dict(cars1=u','.join(list(OrderedDict.fromkeys(filter(lambda x: x != '0', str(alarm_fields['material'][1]).split(',')))))) else: # default aao of current department (without aao) if alarm_fields['city'][1] != 0: # found city -> use default aao c = City.getCities(id=alarm_fields['city'][1]).dept akc = Alarmkey.getDefault(c) alarm.material = dict(cars1=u','.join([str(c.id) for c in akc.cars1]), cars2=u",".join([str(c.id) for c in akc.cars2]), material=u",".join([str(c.id) for c in akc.materials])) l = (u'%s,%s,%s' % (alarm.get('k.cars1'), alarm.get('k.cars2'), alarm.get('k.material'))).split(',') if len(set(str(alarm_fields['material'][1]).split(',')).intersection(set(l))) == 0: _dep = Department.getDefaultDepartment() for c in alarm_fields['material'][1].split(','): if c == u'0': # default of home department needed alarm.material = dict(cars1=u','.join([str(c.id) for c in alarm.key.getCars1(_dep.id)]), cars2=u",".join([str(c.id) for c in alarm.key.getCars2(_dep.id)]), material=u",".join([str(c.id) for c in alarm.key.getMaterial(_dep.id)])) break if u'0' not in alarm_fields['material'][1]: # only single car needed alarm.set('k.cars1', u'{},{}'.format(alarm_fields['material'][1], alarm.get('k.cars1'))) if _ao and _ao.hasOwnAAO(): # use aao of current object alarm.material = dict(cars1=u",".join([str(c.id) for c in _ao.getCars1()]), cars2=u",".join([str(c.id) for c in _ao.getCars2()]), material=u",".join([str(c.id) for c in _ao.getMaterial()])) if 'time' not in kwargs.keys(): kwargs['time'] = [] etime = time.time() kwargs['time'].append('alarm creation done in %s sec.' % (etime - stime)) if kwargs['mode'] != 'test': db.session.add(alarm) db.session.commit() signal.send('alarm', 'added', alarmid=alarm.id) Alarm.changeState(alarm.id, 1) # activate alarm logger.info('alarm created with id %s (%s)' % (alarm.id, (etime - stime))) else: kwargs['fields'] += '\n\n--------------------------\nALARM-Object\n' _cdict = Car.getCarsDict() for a in alarm.attributes: try: if a in ['k.cars1', 'k.cars2', 'k.material']: kwargs['fields'] += '\n-%s:\n %s -> %s' % (a, alarm.get(a), ", ".join([_cdict[int(_c)].name for _c in alarm.get(a).split(',') if _c != ""])) elif a in 'id.key': _k = Alarmkey.getAlarmkeys(id=alarm.get(a)) kwargs['fields'] += '\n-%s:\n %s -> %s: %s' % (a, alarm.get(a), _k.category, _k.key) elif a == 'id.address': kwargs['fields'] += '\n-%s:\n %s -> %s' % (a, alarm.get(a), Street.getStreets(id=alarm.get(a)).name) else: kwargs['fields'] += '\n-%s:\n %s' % (a, alarm.get(a)) except (AttributeError, KeyError): kwargs['fields'] += '\n-%s:\n %s (error)' % (a, alarm.get(a)) kwargs['id'] = '-0' # add dummy id db.session.rollback() logger.info('alarm created in TESTMODE (%s)' % (etime - stime)) return kwargs
def changeState(id, state): """ Change state of alarm with given id. Adds entry in alarmhistory and sends signal :param id: id of alarm :param state: new state as :py:attr:`emonitor.modules.alarms.alarm.Alarm.ALARMSTATES` """ from emonitor.extensions import monitorserver global LASTALARM alarm = Alarm.getAlarms(id=id) if not alarm: return [] if alarm.state != state: # only change _op = 'changestate' else: _op = 'added' if alarm.get('alarmtype', '') != '': _type = '.{}'.format(alarm.get('alarmtype')) else: _type = '' alarm.state = state try: alarm.addHistory('autochangeState', Alarm.ALARMSTATES[str(state)]) except KeyError: alarm.addHistory('autochangeState', 'archived') db.session.commit() if state == 1: # activate alarm c = [] for a in Alarm.getActiveAlarms(): # check cars if a.id == id: continue c.extend(set(a.cars1).intersection(set(alarm.cars1))) c.extend(set(a.cars2).intersection(set(alarm.cars2))) c.extend(set(a.material).intersection(set(alarm.material))) if time.time() - LASTALARM < 60.0: try: ids = [a.id for a in Alarm.getActiveAlarms()] for j in [job for job in scheduler.get_jobs() if job.name == 'changeLayout']: for i in ids: if "'alarmid', %s" % i in str(j.args): # layout changes for given alarm scheduler.unschedule_job(j) except: logger.error('%s' % [a.id for a in Alarm.getActiveAlarms()]) LASTALARM = time.time() alarm.updateSchedules(reference=0) # use current time + delta j = scheduler.add_job(events.raiseEvent, next_run_time=datetime.datetime.fromtimestamp(LASTALARM), args=['alarm_{}{}'.format(_op, _type)], kwargs={'alarmid': id}, name="alarms_activate_{}".format(id)) signal.send('alarm', 'added', alarmid=id) try: flash(babel.gettext(u'alarms.statechangeactivated'), 'alarms.activate') except: pass finally: monitorserver.sendMessage('0', 'reset') # refresh monitor layout #signal.send('alarm', 'changestate', newstate=1) return list(set(c)) elif state == 2: # close alarm LASTALARM = 0.0 alarm.updateSchedules(reference=1) # use alarm.timestamp + delta monitorserver.sendMessage('0', 'reset') # refresh monitor layout signal.send('alarm', 'changestate', newstate=2) return [] elif state == 3: # archive alarm alarm.updateSchedules() signal.send('alarm', 'changestate', newstate=3) return [] signal.send('alarm', 'changestate', newstate=state)
def getAdminData(self, **params): """ Deliver admin content of module maps (ajax) :return: rendered template as string or json dict """ if request.args.get('action') == 'checkpath': if os.path.exists(request.args.get('path')): return '1' return '0' elif request.args.get('action') == 'loadmap': # load tiles tile_path = request.args.get('path') def calcSubItems((pos)): return [(2 * int(pos[0]), 2 * int(pos[1])), (2 * int(pos[0]) + 1, 2 * int(pos[1])), (2 * int(pos[0]), 2 * int(pos[1]) + 1), (2 * int(pos[0]) + 1, 2 * int(pos[1]) + 1)] #def getTile(zoom, pos): # global tile_path # response = urllib2.urlopen('http://a.tile.openstreetmap.org/%s/%s/%s.png' % (zoom, pos[0], pos[1])) # #fout = open(tile_path + str(zoom)+'/'+str(pos[0])+'-'+str(pos[1])+'.png', 'wb') # fout = open("%s%s/%s-%s.png" % (tile_path, zoom, pos[0], pos[1]), 'wb') # fout.write(response.read()) # fout.close() _items = {12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: []} for t in request.args.get('tiles').split("-"): if t != '': _items[12].append(t.split(",")) for zoom in range(13, 19): for i in _items[zoom - 1]: _items[zoom] += calcSubItems(i) result = map_utils.loadTiles('%s%s' % (current_app.config.get('PATH_TILES'), request.args.get('path')), _items) if result == 0: return babel.gettext('settings.map.loadinginprogress') # loading still active elif result == 1: return babel.gettext('settings.map.loadingstarted') # loading started return "" elif request.args.get('action') == 'stoptileload': # stop tile download for i in map_utils.LOADTILES: signal.send('map', 'tiledownloaddone', tiles=i) map_utils.LOADTILES = [] map_utils.CURRENTLOADING = [] return {} elif request.args.get('action') == 'maptiles': _map = Map.getMaps(id=request.args.get('id')) if _map: return Map.getMapBox(tilepath=current_app.config.get('PATH_TILES'), mappath=_map.path) return Map.getMapBox() elif request.args.get('action') == 'loadosmdata': # load all data from openstreetmap from emonitor.extensions import scheduler from emonitor.lib.osm.loaddata import parseOsmData #import time, datetime mapdata = Map.getMaps()[0].getMapBox(tilepath=current_app.config.get('PATH_TILES')) lat = [mapdata['min_latdeg']] while lat[-1] + .05 < mapdata['max_latdeg']: lat.append(lat[-1] + .05) lat.append(mapdata['max_latdeg']) lng = [mapdata['min_lngdeg']] while lng[-1] + .05 < mapdata['max_lngdeg']: lng.append(lng[-1] + .05) lng.append(mapdata['max_lngdeg']) scheduler.add_job(parseOsmData, kwargs={'lat': lat, 'lng': lng, 'path': current_app.config.get('PATH_DATA')}) return {'job': 'started'} elif request.args.get('action') == 'findcity': # search citystring and deliver position return map_utils.loadPositionOfCity(request.args.get('cityname')) return ""
def changeState(id, state): """ Change state of alarm with given id. Adds entry in alarmhistory and sends signal :param id: id of alarm :param state: new state as :py:attr:`emonitor.modules.alarms.alarm.Alarm.ALARMSTATES` """ from emonitor.extensions import monitorserver global LASTALARM alarm = Alarm.getAlarms(id=id) if not alarm: return [] if alarm.state != state and alarm.state != 0: # only change _op = 'changestate' else: _op = 'added' if alarm.get('alarmtype', '') != '': _type = '.{}'.format(alarm.get('alarmtype')) else: _type = '' alarm.state = state try: alarm.addHistory('autochangeState', Alarm.ALARMSTATES[str(state)]) except KeyError: alarm.addHistory('autochangeState', 'archived') db.session.commit() if state == 1: # activate alarm c = [] for a in Alarm.getActiveAlarms(): # check cars if a.id == id: continue c.extend(set(a.cars1).intersection(set(alarm.cars1))) c.extend(set(a.cars2).intersection(set(alarm.cars2))) c.extend(set(a.material).intersection(set(alarm.material))) if time.time() - LASTALARM < 60.0: try: ids = [a.id for a in Alarm.getActiveAlarms()] for j in [job for job in scheduler.get_jobs() if job.name == 'changeLayout']: for i in ids: if "'alarmid', %s" % i in str(j.args): # layout changes for given alarm scheduler.unschedule_job(j) except: logger.error('%s' % [a.id for a in Alarm.getActiveAlarms()]) LASTALARM = time.time() alarm.updateSchedules(reference=0) # use current time + delta j = scheduler.add_job(events.raiseEvent, next_run_time=datetime.datetime.fromtimestamp(LASTALARM), args=['alarm_{}{}'.format(_op, _type)], kwargs={'alarmid': id}, name="alarms_activate_{}".format(id)) signal.send('alarm', _op, alarmid=id, newstate=state) try: flash(babel.gettext(u'alarms.statechangeactivated'), 'alarms.activate') except: pass finally: monitorserver.sendMessage('0', 'reset') # refresh monitor layout return list(set(c)) elif state == 2: # close alarm LASTALARM = 0.0 alarm.updateSchedules(reference=1) # use alarm.timestamp + delta monitorserver.sendMessage('0', 'reset') # refresh monitor layout signal.send('alarm', 'changestate', newstate=2) return [] elif state == 3: # archive alarm alarm.updateSchedules() signal.send('alarm', 'changestate', newstate=3) return [] signal.send('alarm', 'changestate', newstate=state)
def getFrontendContent(**params): """ Deliver frontend content of module alarms :return: data of alarms """ from emonitor.extensions import monitorserver if 'alarmfilter' not in session: session['alarmfilter'] = '7' if request.args.get( 'alarmfilter'): # filter for alarms last x days, -1 no filter set session['alarmfilter'] = request.args.get('alarmfilter', '7') if 'area' in request.args: params['area'] = request.args.get('area') if 'state' in request.args: params['activeacc'] = request.args.get('state') if request.form.get('action') == 'updatealarm': if request.form.get('alarm_id') != 'None': # update alarm alarm = Alarm.getAlarms(request.form.get('alarm_id')) else: # create new alarm d = datetime.datetime.strptime( '%s %s' % (request.form.get('edit_timestamp_date'), request.form.get('edit_timestamp_time')), "%d.%m.%Y %H:%M:%S") alarm = Alarm(d, request.form.get('edit_keyid'), 2, 0) db.session.add(alarm) params['activeacc'] = 1 try: alarm.timestamp = datetime.datetime.strptime( '%s %s' % (request.form.get('edit_timestamp_date'), request.form.get('edit_timestamp_time')), "%d.%m.%Y %H:%M:%S") except ValueError: alarm.timestamp = datetime.datetime.now() alarm._key = request.form.get('edit_key') alarm.set(u'id.key', request.form.get('edit_keyid')) alarm.set(u'k.cars1', request.form.get('val_cars1')) alarm.set(u'k.cars2', request.form.get('val_cars2')) alarm.set(u'k.material', request.form.get('val_material')) alarm.set(u'marker', request.form.get('marker')) alarm.set(u'id.city', request.form.get('edit_city')) _city = City.getCities(id=request.form.get('edit_cityname')) if _city: alarm.set(u'city', _city.name) else: alarm.set(u'city', request.form.get('edit_cityname')) alarm.set(u'streetno', request.form.get('edit_streetno')) street = Street.getStreets(id=request.form.get('edit_addressid')) hnumber = None if street: alarm.set(u'id.address', street.id) try: hnumber = [ h for h in street.housenumbers if h.number == request.form.get('edit_streetno').split()[0] ] if len(hnumber) > 0: alarm.set(u'lat', hnumber[0].points[0][0]) alarm.set(u'lng', hnumber[0].points[0][1]) except IndexError: pass elif request.form.get('edit_addressid') == 'None': alarm.set(u'id.address', '') else: alarm.set(u'id.address', request.form.get('edit_addressid')) alarm.set(u'address', request.form.get('edit_address')) if request.form.get('edit_object') != '0': alarm.set(u'id.object', request.form.get('edit_object')) else: # remove object definition if present if u'id.object' in alarm.attributes: del alarm.attributes[u'id.object'] if u'object' in alarm.attributes: del alarm.attributes[u'object'] alarm.set(u'priority', request.form.get('edit_priority')) alarm.set(u'remark', request.form.get('edit_remark')) alarm.set(u'person', request.form.get('edit_person')) if request.form.get(u'edit_address2').strip() != '': alarm.set(u'address2', request.form.get('edit_address2')) if (request.form.get(u'marker') == '1' and not hnumber) or request.form.get('update_position') == '1': alarm.set(u'routing', '') alarm.set(u'lat', request.form.get('lat')) alarm.set(u'lng', request.form.get('lng')) alarm.set(u'zoom', request.form.get('zoom')) try: d = datetime.datetime.strptime( '%s %s' % (request.form.get('edit_endtimestamp_date'), request.form.get('edit_endtimestamp_time')), "%d.%m.%Y %H:%M:%S") except ValueError: d = datetime.datetime.now() alarm.set(u'endtimestamp', d) db.session.commit() signal.send('alarm', 'updated', alarmid=alarm.id) if request.form.get('alarm_id') == u'None': # create new Alarm.changeState(alarm.id, 0) # prepare alarm return redirect('/alarms?area=%s&state=1' % params['area']) elif alarm.state == 1: # active alarm update monitorserver.sendMessage('0', 'reset') # refresh monitor layout return redirect('/alarms?area=%s&state=0' % params['area']) elif request.args.get('action') == 'editalarm': if request.args.get('alarmid', '0') == '0': # add new alarm alarm = Alarm(datetime.datetime.now(), '', 2, 0) #flash(babel.gettext(u'alarms.alarmadded'), 'alarms.add') else: # edit alarm alarm = Alarm.getAlarms(id=request.args.get('alarmid')) return render_template( 'frontend.alarms_edit.html', alarm=alarm, cities=City.getCities(), objects=AlarmObject.getAlarmObjects(), cars=Car.getCars(), departments=Department.getDepartments(), frontendarea=params['area'], frontendmodules=frontend.modules, frontendmoduledef=Settings.get('frontend.default')) elif request.args.get('action') == 'refresh': # refresh alarm section params['area'] = request.args.get('area') params['activeacc'] = int(request.args.get('activeacc')) elif request.args.get('action') == 'finishalarm': # finish selected alarm Alarm.changeState(int(request.args.get('alarmid')), 2) params['area'] = request.args.get('area') elif request.args.get( 'action') == 'activatealarm': # activate selected alarm ret = Alarm.changeState(int(request.args.get('alarmid')), 1) if len(ret) > 0: flash( render_template_string( "{{ _('alarms.carsinuse') }}</br><b>" + ", ".join( [r.name for r in sorted(ret, key=attrgetter('name'))]) + "</b>"), 'alarms') params['area'] = request.args.get('area') params['activeacc'] = 0 elif request.args.get('action') == 'deletealarm': # delete selected alarm alarm = Alarm.getAlarms(id=request.args.get('alarmid')) refresh = 1 or alarm.state == 1 # check if alarm is active try: # delete file if not used in any other alarm c = Alarm.query.filter( Alarm.attributes.any(value=alarm.get('filename'), name="filename")).count() if c == 1 and os.path.exists("{}{}".format( current_app.config.get('PATH_DONE'), alarm.get('filename'))): os.remove("{}{}".format(current_app.config.get('PATH_DONE'), alarm.get('filename'))) except: pass alarm.state = -1 alarm.updateSchedules() db.session.delete(alarm) db.session.commit() if refresh: monitorserver.sendMessage('0', 'reset') # refresh monitor layout signal.send('alarm', 'deleted', alarmid=request.args.get('alarmid')) elif request.args.get( 'action' ) == 'archivealarm': # archive selected alarms, id=0 == all if ";" in request.args.get('alarmid'): # archive selected alarms for alarmid in request.args.get('alarmid').split(';'): Alarm.changeState(int(alarmid), 3) elif int(request.args.get('alarmid')) == 0: # archive all alarms Alarm.changeStates(3) else: # archive single selected alarm Alarm.changeState(int(request.args.get('alarmid')), 3) params['area'] = request.args.get('area') stats = dict.fromkeys(Alarm.ALARMSTATES.keys() + ['3'], 0) for s, c in Alarm.getAlarmCount(days=int( session['alarmfilter'])): # s=state, c=count(ids of state) if str(s) in stats.keys(): stats[str(s)] = c if 'area' not in params: params['area'] = 'center' if 'activeacc' not in params: params['activeacc'] = 0 return render_template( 'frontend.alarms_smallarea.html', alarmstates=Alarm.ALARMSTATES, stats=stats, frontendarea=params['area'], activeacc=str(params['activeacc']), printdefs=Printers.getActivePrintersOfModule('alarms'), frontendmodules=frontend.modules, frontendmoduledef=Settings.get('frontend.default'), alarmfilter=session['alarmfilter'])
def handleEvent(eventname, **kwargs): """ Eventhandler for alarm class :param eventname: name of event :param kwargs: parameter list: error, fields, filename, id, incomepath, mode, time :return: all kwargs """ try: from emonitor import app global LASTALARM alarm_fields = dict() stime = time.time() alarmtype = None for t in AlarmType.getAlarmTypes(): if re.search(t.keywords.replace('\r\n', '|'), kwargs.get('text', '')): alarm_fields = t.interpreterclass().buildAlarmFromText(t, kwargs.get('text', '')) if alarm_fields.get('error'): kwargs['error'] = alarm_fields['error'] del alarm_fields['error'] alarmtype = t break alarm = Alarm(datetime.datetime.now(), '', 1, 0) etime = time.time() _missing = 0 for p in ['time', 'city', 'address', 'key']: if p not in alarm_fields: # test required fields _missing += 1 kwargs['error'] = kwargs.get('error', 'Missing parameter:') + "<br/>- '{}'".format(p) try: t = datetime.datetime.strptime(alarm_fields.get('time')[0], '%d.%m.%Y - %H:%M:%S') except ValueError: t = datetime.datetime.now() alarm.timestamp = t kwargs['id'] = u'<ul><li>-test-</li></ul>' # add dummy id kwargs['fields'] = u'<ul>' # set evaluated fields from fax for k in sorted(alarm_fields): # add field values as unicode string if len(alarm_fields[k]) > 1: kwargs['fields'] = u'{}<li><b>{}:</b>\n <small style="color:silver">value:</small> "{}" → <small style="color:silver">id:</small> {}</li>'.format(kwargs.get('fields'), k, alarm_fields[k][0], alarm_fields[k][1]) else: kwargs['fields'] = u'{}<li><b>{}:</b>\n <small style="color:silver">value:</small> "{}"</li>'.format(kwargs.get('fields'), k, alarm_fields[k]) kwargs['fields'] = kwargs.get('fields', ' ') + '</ul></pre>\n\n<h5> ALARM-Object</h5>\n<pre><ul>' if _missing == 0: # all required parameters found if kwargs.get('mode') != 'test': if not os.path.exists('{}{}'.format(app.config.get('PATH_DONE'), t.strftime('%Y/%m/'))): os.makedirs('{}{}'.format(app.config.get('PATH_DONE'), t.strftime('%Y/%m/'))) try: shutil.copy2('{incomepath}{filename}'.format(**kwargs), '{}{}{}'.format(app.config.get('PATH_DONE'), t.strftime('%Y/%m/%Y%m%d-%H%M%S'), os.path.splitext(kwargs.get('filename'))[1])) except: pass try: # remove file os.remove('{incomepath}{filename}'.format(**kwargs)) except: pass kwargs['filename'] = '{}{}'.format(t.strftime('%Y/%m/%Y%m%d-%H%M%S'), os.path.splitext(kwargs['filename'])[1]) logger.debug('alarm_fields: {}'.format(alarm_fields)) if len(alarm_fields) == 0: # no alarmfields found kwargs['id'] = 0 logger.error('no alarm fields found.') return kwargs if not alarmtype: # alarmtype not found kwargs['id'] = 0 kwargs['error'] = kwargs.get('error', '') + 'alarmtype not found' logger.error('alarmtype not found.') return kwargs # position if alarm_fields.get('lat'): _position = dict(lat=alarm_fields.get('lat')[0], lng=alarm_fields.get('lng')[0]) else: _position = dict(lat=u'0.0', lng=u'0.0') if USE_NOMINATIM == 1: try: url = 'http://nominatim.openstreetmap.org/search' params = 'format=json&city={}&street={}'.format(alarm_fields['city'][0], alarm_fields['address'][0]) if 'streetno' in alarm_fields: params += ' {}'.format(alarm_fields['streetno'][0].split()[0]) # only first value r = requests.get('{}?{}'.format(url, params)) _position = dict(lat=r.json()[0]['lat'], lng=r.json()[0]['lon']) except: pass # create alarm object if alarm_fields.get('key', [u'', 0])[0] == u'': if alarmtype.translation(u'_bma_main_') in alarm_fields.get('remark', [u'', 0])[0] or alarmtype.translation(u'_bma_main_') in alarm_fields.get('person', [u'', 0])[0]: alarmkey = Alarmkey.query.filter(Alarmkey.key.like(u"%{}%".format(alarmtype.translation(u'_bma_')))).all() if len(alarmkey) > 0: alarm_fields['key'] = ('{}: {}'.format(alarmkey[0].category, alarmkey[0].key), str(alarmkey[0].id)) else: alarm_fields['key'] = (alarmtype.translation(u'_bma_key_'), u'0') if alarm_fields.get('time', [u'', 0])[1] == 1: # found correct time t = datetime.datetime.strptime(alarm_fields.get('time', [u'', 0])[0], '%d.%m.%Y - %H:%M:%S') else: t = datetime.datetime.now() alarm.timestamp = t alarm.set('id.key', alarm_fields['key'][1]) alarm._key = alarm_fields['key'][0] alarm.material = dict(cars1='', cars2='', material='') # set required attributes alarm.set('marker', '0') alarm.set('filename', kwargs['filename']) alarm.set('priority', '1') # set normal priority alarm.set('alarmtype', alarmtype.name) # set checker name alarm.position = _position alarm.state = 1 # city if alarm_fields.get('city', ['', 0])[1] > 0: alarm.city = City.getCities(id=alarm_fields.get('city')[1]) if alarm_fields.get('address'): alarm.street = Street.getStreets(id=alarm_fields.get('address')[1]) else: # city not found -> build from fax url = 'http://nominatim.openstreetmap.org/search' if len(alarm_fields.get('city', [u'', 0])[0].split()) > 0: params = u'format=json&city={}&street={}'.format(alarm_fields.get('city', [u'', 0])[0].split()[0], alarm_fields.get('address', [u'', 0])[0]) if alarm_fields.get('streetno'): try: params += u' {}'.format(alarm_fields.get('streetno')[0].split()[0]) # only first value except: pass alarm.set('streetno', alarm_fields.get('streetno')[0]) try: r = requests.get(u'{}?{}'.format(url, params)) logger.debug(u'load address data from nomination with parameters: city={} street={}'.format(alarm_fields.get('city')[0].split()[0], alarm_fields.get('address', ['', 0])[0])) _position = dict(lat=r.json()[0]['lat'], lng=r.json()[0]['lon']) alarm.position = _position except: pass if len(alarm_fields.get('city', ['', 0])[0].split()) > 0: alarm.set('city', alarm_fields.get('city', ['', 0])[0].split()[0]) alarm.set('id.city', alarm_fields.get('city')[1]) alarm.set('address', alarm_fields.get('address', ['', 0])[0]) if alarm_fields.get('address', [u'', 0])[1] != 0: alarm.street = Street.getStreets(id=alarm_fields.get('address')[1]) if alarm_fields.get('cars'): # add cars found in material for _c in alarm_fields.get('cars')[1].split(';'): alarm.set('k.cars1', alarm.get('k.cars1') + ';' + _c) # street / street2 if alarm_fields.get('address', [u'', 0]) != '': # check correct city -> change if street has different city if len(str(alarm_fields.get('address')[1]).split(';')) > 0 and alarm_fields.get('address')[1] != 0: _c = [] for s in str(alarm_fields.get('address')[1]).split(';'): _s = Street.getStreets(id=s) if _s.cityid and _s.cityid not in _c and _s.cityid == alarm_fields.get('city', [u'', 0])[1]: _c.append(_s.cityid) alarm.street = _s if alarm_fields.get('object', [u'', 0])[1] == 0: if not alarm_fields.get('lat') and not alarm_fields.get('lng'): alarm.position = dict(lat=_s.lat, lng=_s.lng, zoom=_s.zoom) if _position['lat'] != u'0.0' and _position['lng'] != u'0.0': # set marker if nominatim delivers result alarm.position = _position alarm.set('marker', '1') else: # add unknown street alarm.set('id.address', 0) alarm.set('address', alarm_fields['address'][0]) # houseno if alarm_fields.get('streetno'): alarm.set('streetno', alarm_fields.get('streetno')[0]) if alarm_fields.get('id.streetno') and alarm_fields.get('lat') and alarm_fields.get('lng'): alarm.position = dict(lat=alarm_fields.get('lat')[0], lng=alarm_fields.get('lng')[0]) alarm.set('id.streetno', alarm_fields.get('id.streetno')[1]) else: # new hn = alarm.street.getHouseNumber(name=alarm_fields.get('streetno')[0]) if hn: alarm.position = hn.getPosition(0) if alarm_fields.get('zoom'): alarm.set('zoom', alarm_fields.get('zoom')[0]) # crossing if alarm_fields.get('crossing', [u'', 0])[0] != '': if alarm_fields.get('crossing', [u'', 0])[1] != alarm_fields.get('address',[u'', 0])[1]: alarm.set('id.address2', alarm_fields.get('crossing')[1]) alarm.set('address2', alarm_fields.get('crossing')[0]) else: alarm.set('id.address2', '0') alarm.set('address2', alarm_fields.get('crossing')[0]) # addresspart if alarm_fields.get('addresspart', [u'', 0])[0] != u'' and alarm_fields.get('addresspart', [u'', 0])[0] != alarm_fields.get('address', [u'', 0])[0]: if alarm_fields.get('addresspart')[1] > 0: if len(str(alarm_fields.get('addresspart')[1]).split(';')) > 0: _c = [] for s in str(alarm_fields.get('addresspart')[1]).split(';'): try: _s = Street.getStreets(id=s) if _s.cityid not in _c and _s.cityid == alarm_fields.get('city')[1]: _c.append(_s.cityid) alarm.set('id.address2', _s.id) except: pass else: alarm.set('id.address2', alarm_fields.get('addresspart')[1]) else: alarm.set('id.address2', '0') alarm.set('address2', alarm_fields.get('addresspart')[0]) # person if alarm_fields.get('person', [u'', 0])[0] != u'': alarm.set('person', alarm_fields.get('person')[0]) # alarmplan if alarm_fields.get('alarmplan', [u'', 0])[0] != u'': alarm.set('alarmplan', alarm_fields.get('alarmplan')[0]) # alarmobject _ao = None if alarm_fields.get('object', [u'', 0])[0] != u'' and alarm_fields.get('city', [u'', 0])[1] > 0: alarm.set('object', alarm_fields.get('object')[0]) alarm.set('id.object', alarm_fields.get('object')[1]) # alarmplan from object if alarm_fields.get('object')[1] != 0: _ao = AlarmObject.getAlarmObjects(id=alarm_fields.get('object')[1]) if _ao: if _ao.alarmplan != 0: alarm.set('alarmplan', _ao.alarmplan) elif alarm_fields.get('alarmplan'): alarm.set('alarmplan', alarm_fields.get('alarmplan')[0]) if _ao.street.id != alarm_fields.get('address', [u'', 0])[1]: # street config from alarmobject alarm.street = Street.getStreets(id=_ao.street.id) if _ao.streetno == "": alarm.set('streetno', alarm_fields.get('streetno')[0]) else: alarm.set('streetno', _ao.streetno) alarm.position = dict(lat=_ao.lat, lng=_ao.lng, zoom=_ao.zoom) # remark if alarm_fields.get('remark', [u'', 0])[0] != u'': alarm.set('remark', alarm_fields.get('remark')[0]) _bma = 0 for v in alarmtype.translation(u'_bma_main_').split(): if v in alarm_fields.get('remark', [u'', 0])[0] or v in alarm_fields.get('person', [u'', 0])[0]: _bma = 1 break if _bma == 1: alarmkey = Alarmkey.query.filter(Alarmkey.key.like(u"%{}%".format(alarmtype.translation(u'_bma_')))).first() if alarmkey: alarm.set('id.key', alarmkey.id) alarm._key = u'{}: {}'.format(alarmkey.category, alarmkey.key) else: alarm.set('id.key', '0') alarm._key = alarmtype.translation(u'_bma_key_') # additional remarks if alarm_fields.get('remark2', [u'', 0])[0] != u'': alarm.set('remark', u'{}\n{}'.format(alarm.get('remark'), alarm_fields.get('remark2')[0])) # material if alarm.get('id.key') != 0 and alarm_fields.get('city'): # found key with aao if alarm_fields.get('city')[1] not in [0, -1]: # default city if Department.getDepartments(id=alarm.city.dept).defaultcity == alarm_fields.get('city')[1]: # default city for dep if alarm_fields.get('material'): if str(alarm_fields.get('material')[1])[0] == '0': # default cars for aao try: alarm.material = dict(cars1=u','.join([str(c.id) for c in alarm.key.getCars1(alarm.street.city.dept)]), cars2=u','.join([str(c.id) for c in alarm.key.getCars2(alarm.street.city.dept)]), material=u','.join([str(c.id) for c in alarm.key.getMaterial(alarm.street.city.dept)])) except AttributeError: alarm.material = dict(cars1=u','.join([str(c.id) for c in alarm.key.getCars1(alarm.city.dept)]), cars2=u','.join([str(c.id) for c in alarm.key.getCars2(alarm.city.dept)]), material=u','.join([str(c.id) for c in alarm.key.getMaterial(alarm.city.dept)])) for _c in u'{}'.format(alarm_fields.get('material')[1]).split(','): # add additional cars if _c != '0' and _c not in alarm.get('k.cars1').split(','): alarm.set('k.cars1', u'{},{}'.format(alarm.get('k.cars1'), _c)) else: # only alarmed material alarm.material = dict(cars1=alarm_fields.get('material', [u'', ''])[1]) else: # else city if alarm_fields.get('material', [u'', u''])[1] == u'0': # default cars for aao alarm.material = dict(cars1=u','.join([str(c.id) for c in alarm.key.getCars1(alarm.city.dept)]), cars2=u','.join([str(c.id) for c in alarm.key.getCars2(alarm.city.dept)]), material=u','.join([str(c.id) for c in alarm.key.getMaterial(alarm.city.dept)])) else: alarm.material = dict(cars1=u','.join(list(OrderedDict.fromkeys(filter(lambda x: x != '0', str(alarm_fields.get('material', ['', '-1'])[1]).split(',')))))) else: # default aao of current department (without aao) if alarm_fields.get('city', [u'', 0])[1] != 0: # found city -> use default aao if City.getCities(id=alarm_fields.get('city')[1]): c = City.getCities(id=alarm_fields.get('city')[1]).dept else: c = City.getDefaultCity().dept akc = Alarmkey.getDefault(c) if len(akc.cars1) + len(akc.cars2) + len(akc.materials) == 0: # no default aao defined # use cars of fax alarm.material = {'cars1': re.sub(r'^0,', '', alarm_fields.get('material')[1]), 'cars2': '', 'material': ''} else: # use cars of default aao alarm.material = dict(cars1=u','.join([str(c.id) for c in akc.cars1]), cars2=u",".join([str(c.id) for c in akc.cars2]), material=u",".join([str(c.id) for c in akc.materials])) l = (u'{},{},{}'.format(alarm.get('k.cars1'), alarm.get('k.cars2'), alarm.get('k.material'))).split(',') if len(set(str(alarm_fields.get('material', ([], '-1'))[1]).split(',')).intersection(set(l))) == 0: _dep = Department.getDefaultDepartment() for c in str(alarm_fields.get('material', ([], '-1'))[1]).split(','): if c == u'0': # default of home department needed alarm.material = dict(cars1=u','.join([str(c.id) for c in alarm.key.getCars1(_dep.id)]), cars2=u",".join([str(c.id) for c in alarm.key.getCars2(_dep.id)]), material=u",".join([str(c.id) for c in alarm.key.getMaterial(_dep.id)])) break if u'0' not in str(alarm_fields.get('material', ([], '-1'))[1]): # only single car needed alarm.set('k.cars1', u'{},{}'.format(alarm_fields.get('material', ([], '-1'))[1], alarm.get('k.cars1'))) if _ao and _ao.hasOwnAAO(): # use aao of current object alarm.material = dict(cars1=u",".join([str(c.id) for c in _ao.getCars1()]), cars2=u",".join([str(c.id) for c in _ao.getCars2()]), material=u",".join([str(c.id) for c in _ao.getMaterial()])) if not kwargs.get('time'): kwargs['time'] = [] kwargs['time'].append('alarm creation done in {} sec.'.format(etime - stime)) if kwargs.get('mode') != 'test': db.session.add(alarm) db.session.commit() signal.send('alarm', 'added', alarmid=alarm.id) Alarm.changeState(alarm.id, 1) # activate alarm logger.info('alarm created with id {} ({})'.format(alarm.id, (etime - stime))) else: _cdict = Car.getCarsDict() for a in sorted(alarm.attributes): try: if a in ['k.cars1', 'k.cars2', 'k.material']: kwargs['fields'] += '<li><b>%s:</b>\n <small style="color:silver">id:</small> "%s" → "%s"</li>' % (a, alarm.get(a), ", ".join([_cdict[int(_c)].name for _c in alarm.get(a).split(',') if _c not in ['', '0']])) elif a in 'id.key': if alarm.get(a) > 0: _k = Alarmkey.getAlarmkeys(id=alarm.get(a)) kwargs['fields'] += '<li><b>%s:</b>\n <small style="color:silver">id:</small> "%s" → "%s: %s"</li>' % (a, alarm.get(a), _k.category, _k.key) else: kwargs['fields'] += '<li><b>%s:</b>\n <small style="color:silver">value:</small> "%s"</li>' % (a, alarm.get(a)) kwargs['fields'] += '<li><b>key:</b>\n <small style="color:silver">value:</small> "%s"</li>' % alarm._key elif a == 'id.address': kwargs['fields'] += '<li><b>%s:</b>\n <small style="color:silver">id:</small> "%s" → "%s"</li>' % (a, alarm.get(a), Street.getStreets(id=alarm.get(a)).name) elif a == 'id.object': kwargs['id.object'] = '<li><b>%s:</b>\n <small style="color:silver">value:</small> "%s"</li>' % (a, alarm.get(a)) kwargs['object'] = '<li><b>object:</b>\n <small style="color:silver">value:</small> "%s"</li>' % alarm.get('object') else: kwargs['fields'] += '<li><b>%s:</b>\n <small style="color:silver">value:</small> "%s"</li>' % (a, alarm.get(a)) except (AttributeError, KeyError): kwargs['fields'] += '<li style="color:red"><b>%s:</b>\n <small style="color:silver">value:</small> "%s" (error)</li>' % (a, alarm.get(a)) kwargs['fields'] += "</ul>" db.session.rollback() logger.info('alarm created in TESTMODE (%s)' % (etime - stime)) except: signal.send('alarm', 'error', message='alarms.errorincreation', text=kwargs.get('text', '')) return kwargs
def loadTiles(path, tilelist): """ Load map tiles into path from given tilelist :param path: path to store map tiles :param tilelist: list of tiles to load from OSM :return: progress information *[position, number of tiles to load]* as list """ from emonitor.extensions import scheduler LOADTILES.append(tilelist[min(tilelist.keys())][0]) # add selected tile CURRENTLOADING.extend(sum(tilelist.values(), [])) # add tiles signal.send('map', 'tiledownloadstart', tiles=tilelist[min(tilelist.keys())][0]) def doLoadTiles(**kwargs): def getTile(zoom, item): response = urllib2.urlopen( 'http://a.tile.openstreetmap.org/{}/{}/{}.png'.format( zoom, item[0], item[1])) with open('{}/{}/{}-{}.png'.format(path, zoom, item[0], item[1]), 'wb') as fout: fout.write(response.read()) if item in CURRENTLOADING: CURRENTLOADING.remove(item) if 'path' in kwargs: path = kwargs['path'] else: return if 'tilelist' in kwargs: tilelist = kwargs['tilelist'] else: return errortiles = [] for zoom in tilelist: if not os.path.exists('{}/{}'.format(path, zoom)): os.makedirs('{}/{}'.format(path, zoom)) for item in tilelist[zoom]: if len(CURRENTLOADING) == 0: # loding stopped or ended return if (len(LOADTILES) * 5460 - len(CURRENTLOADING) ) % 10 == 0: # send state every 10 loads signal.send( 'map', 'tiledownloadprogress', progress=(len(LOADTILES) * 5460 - len(CURRENTLOADING), len(LOADTILES) * 5460), tiles=LOADTILES) try: getTile(zoom, item) except: errortiles.append((zoom, item)) # try error objects for err in errortiles: try: getTile(err[0], err[1]) except: print "error in {}".format(err) signal.send('map', 'tiledownloaddone', tiles=tilelist[min(tilelist.keys())][0]) scheduler.add_job(doLoadTiles, kwargs={'path': path, 'tilelist': tilelist}) return 1 # loading started
def getFrontendContent(**params): """ Deliver frontend content of module alarms :return: data of alarms """ from emonitor.extensions import monitorserver if 'alarmfilter' not in session: session['alarmfilter'] = '7' if request.args.get('alarmfilter'): # filter for alarms last x days, -1 no filter set session['alarmfilter'] = request.args.get('alarmfilter', '7') if 'area' in request.args: params['area'] = request.args.get('area') if 'state' in request.args: params['activeacc'] = request.args.get('state') if request.form.get('action') == 'updatealarm': if request.form.get('alarm_id') != 'None': # update alarm alarm = Alarm.getAlarms(request.form.get('alarm_id')) else: # create new alarm d = datetime.datetime.strptime('%s %s' % (request.form.get('edit_timestamp_date'), request.form.get('edit_timestamp_time')), "%d.%m.%Y %H:%M:%S") alarm = Alarm(d, request.form.get('edit_keyid'), 2, 0) db.session.add(alarm) params['activeacc'] = 1 try: alarm.timestamp = datetime.datetime.strptime('%s %s' % (request.form.get('edit_timestamp_date'), request.form.get('edit_timestamp_time')), "%d.%m.%Y %H:%M:%S") except ValueError: alarm.timestamp = datetime.datetime.now() alarm._key = request.form.get('edit_key') alarm.set(u'id.key', request.form.get('edit_keyid')) alarm.set(u'k.cars1', request.form.get('val_cars1')) alarm.set(u'k.cars2', request.form.get('val_cars2')) alarm.set(u'k.material', request.form.get('val_material')) alarm.set(u'marker', request.form.get('marker')) alarm.set(u'id.city', request.form.get('edit_city')) _city = City.getCities(id=request.form.get('edit_cityname')) if _city: alarm.set(u'city', _city.name) else: alarm.set(u'city', request.form.get('edit_cityname')) alarm.set(u'streetno', request.form.get('edit_streetno')) street = Street.getStreets(id=request.form.get('edit_addressid')) hnumber = None if street: alarm.set(u'id.address', street.id) try: hnumber = [h for h in street.housenumbers if h.number == request.form.get('edit_streetno').split()[0]] if len(hnumber) > 0: alarm.set(u'lat', hnumber[0].points[0][0]) alarm.set(u'lng', hnumber[0].points[0][1]) except IndexError: pass elif request.form.get('edit_addressid') == 'None': alarm.set(u'id.address', '') else: alarm.set(u'id.address', request.form.get('edit_addressid')) alarm.set(u'address', request.form.get('edit_address')) if request.form.get('edit_object') != '0': alarm.set(u'id.object', request.form.get('edit_object')) else: # remove object definition if present if u'id.object' in alarm.attributes: del alarm.attributes[u'id.object'] if u'object' in alarm.attributes: del alarm.attributes[u'object'] alarm.set(u'priority', request.form.get('edit_priority')) alarm.set(u'remark', request.form.get('edit_remark')) alarm.set(u'person', request.form.get('edit_person')) if request.form.get(u'edit_address2').strip() != '': alarm.set(u'address2', request.form.get('edit_address2')) if (request.form.get(u'marker') == '1' and not hnumber) or request.form.get('update_position') == '1': alarm.set(u'routing', '') alarm.set(u'lat', request.form.get('lat')) alarm.set(u'lng', request.form.get('lng')) alarm.set(u'zoom', request.form.get('zoom')) try: d = datetime.datetime.strptime('%s %s' % (request.form.get('edit_endtimestamp_date'), request.form.get('edit_endtimestamp_time')), "%d.%m.%Y %H:%M:%S") except ValueError: d = datetime.datetime.now() alarm.set(u'endtimestamp', d) db.session.commit() signal.send('alarm', 'updated', alarmid=alarm.id) if request.form.get('alarm_id') == u'None': # create new Alarm.changeState(alarm.id, 0) # prepare alarm return redirect('/alarms?area=%s&state=1' % params['area']) elif alarm.state == 1: # active alarm update monitorserver.sendMessage('0', 'reset') # refresh monitor layout return redirect('/alarms?area=%s&state=0' % params['area']) elif request.args.get('action') == 'editalarm': if request.args.get('alarmid', '0') == '0': # add new alarm alarm = Alarm(datetime.datetime.now(), '', 2, 0) #flash(babel.gettext(u'alarms.alarmadded'), 'alarms.add') else: # edit alarm alarm = Alarm.getAlarms(id=request.args.get('alarmid')) return render_template('frontend.alarms_edit.html', alarm=alarm, cities=City.getCities(), objects=AlarmObject.getAlarmObjects(), cars=Car.getCars(), departments=Department.getDepartments(), frontendarea=params['area'], frontendmodules=frontend.modules, frontendmoduledef=Settings.get('frontend.default')) elif request.args.get('action') == 'refresh': # refresh alarm section params['area'] = request.args.get('area') params['activeacc'] = int(request.args.get('activeacc')) elif request.args.get('action') == 'finishalarm': # finish selected alarm Alarm.changeState(int(request.args.get('alarmid')), 2) params['area'] = request.args.get('area') elif request.args.get('action') == 'activatealarm': # activate selected alarm ret = Alarm.changeState(int(request.args.get('alarmid')), 1) if len(ret) > 0: flash(render_template_string("{{ _('alarms.carsinuse') }}</br><b>" + ", ".join([r.name for r in sorted(ret, key=attrgetter('name'))]) + "</b>"), 'alarms') params['area'] = request.args.get('area') params['activeacc'] = 0 elif request.args.get('action') == 'deletealarm': # delete selected alarm alarm = Alarm.getAlarms(id=request.args.get('alarmid')) refresh = 1 or alarm.state == 1 # check if alarm is active try: # delete file if not used in any other alarm c = Alarm.query.filter(Alarm.attributes.any(value=alarm.get('filename'), name="filename")).count() if c == 1 and os.path.exists("{}{}".format(current_app.config.get('PATH_DONE'), alarm.get('filename'))): os.remove("{}{}".format(current_app.config.get('PATH_DONE'), alarm.get('filename'))) except: pass alarm.state = -1 alarm.updateSchedules() db.session.delete(alarm) db.session.commit() if refresh: monitorserver.sendMessage('0', 'reset') # refresh monitor layout signal.send('alarm', 'deleted', alarmid=request.args.get('alarmid')) elif request.args.get('action') == 'archivealarm': # archive selected alarms, id=0 == all if ";" in request.args.get('alarmid'): # archive selected alarms for alarmid in request.args.get('alarmid').split(';'): Alarm.changeState(int(alarmid), 3) elif int(request.args.get('alarmid')) == 0: # archive all alarms Alarm.changeStates(3) else: # archive single selected alarm Alarm.changeState(int(request.args.get('alarmid')), 3) params['area'] = request.args.get('area') stats = dict.fromkeys(Alarm.ALARMSTATES.keys() + ['3'], 0) for s, c in Alarm.getAlarmCount(days=int(session['alarmfilter'])): # s=state, c=count(ids of state) if str(s) in stats.keys(): stats[str(s)] = c if 'area' not in params: params['area'] = 'center' if 'activeacc' not in params: params['activeacc'] = 0 return render_template('frontend.alarms_smallarea.html', alarmstates=Alarm.ALARMSTATES, stats=stats, frontendarea=params['area'], activeacc=str(params['activeacc']), printdefs=Printers.getActivePrintersOfModule('alarms'), frontendmodules=frontend.modules, frontendmoduledef=Settings.get('frontend.default'), alarmfilter=session['alarmfilter'])