def create(self, validated_data): ret = self.process_data(validated_data) # print(ret) bid = validated_data['CarID'] try: bus = Bus.objects.get(bid=bid) except Exception: bus = Bus.objects.create(bid=bid) cache_obj = busStorage.get(bid) # print(cache_obj) now = timezone.now() for modelName, objs in ret.items(): if modelName == 'Bus': continue # update cache obj if modelName == 'EnergySavingData': checkinStorage.publish_busdata(bid, objs) old = cache_obj.get(modelName, {}) for key, value in objs.items(): try: objs[key] = old.get(key, 0) + float(value) except ValueError: logger.error("Failed to cast %s to float" % key) cache_obj[modelName].update(objs) cache_obj[modelName]['timestamp'] = now model = apps.get_model(app_label='vehicle', model_name=modelName) objs['bus'] = bus objs['timestamp'] = now model.objects.create(**objs) # print(cache_obj) busStorage.set(bid, cache_obj) return bus
def busData(request, bid): data = busStorage.get(bid) return Response(data)