From 4b4c1ac56e03683b1df83479b48f97e076ef1a89 Mon Sep 17 00:00:00 2001 From: Mason DeMelo Date: Wed, 27 Feb 2019 13:13:53 -0500 Subject: [PATCH] API - initial commit --- API/DataObjects.py | 40 ++++++ API/DataObjects.pyc | Bin 0 -> 2195 bytes API/__pycache__/DataObjects.cpython-36.pyc | Bin 0 -> 1914 bytes API/api.py | 146 +++++++++++++++++++++ API/config.ini | 7 + API/db/init_sqlite.sql | 14 ++ API/db/test.db | Bin 0 -> 12288 bytes API/post_test.py | 44 +++++++ 8 files changed, 251 insertions(+) create mode 100644 API/DataObjects.py create mode 100644 API/DataObjects.pyc create mode 100644 API/__pycache__/DataObjects.cpython-36.pyc create mode 100644 API/api.py create mode 100644 API/config.ini create mode 100644 API/db/init_sqlite.sql create mode 100644 API/db/test.db create mode 100644 API/post_test.py diff --git a/API/DataObjects.py b/API/DataObjects.py new file mode 100644 index 0000000..7855755 --- /dev/null +++ b/API/DataObjects.py @@ -0,0 +1,40 @@ +from sqlalchemy import create_engine +from sqlalchemy import Column, Integer, String, Binary, Float +from sqlalchemy.orm import relationship, sessionmaker, deferred +from sqlalchemy.orm.collections import attribute_mapped_collection +from sqlalchemy.ext.declarative import declarative_base + +from datetime import datetime + +Base = declarative_base() + +class LocationData(Base): + """ + Location Data for HuskyTrack + + Attributes: + uuid: The Location Data's universally unique identifier + uid: The user ID of the user this data belongs to + latitude: The latitude of the device when this measurement was taken + longitude: The longitude of the device when this measurement was taken + accuracy: The accuracy of the measurement as reported by the device + speed: The speed of the device when this measurement was taken + heading: The heading of the device when this measurement was taken + altitude: The altitude of the device when this measurement was taken + timestamp: The time at which this measurement was taken [ISO-8601 UTC] + """ + + __tablename__ = 'location_data' + + uuid = Column('uuid', String, primary_key=True) + uid = Column('uid', String, nullable=False) + latitude = Column('latitude', Float, nullable=False) + longitude = Column('longitude', Float, nullable=False) + accuracy = Column('accuracy', Float, nullable=False) + speed = Column('speed', Float, nullable=False) + heading = Column('heading', Float, nullable=False) + altitude = Column('altitude', Float, nullable=False) + timestamp = Column('timestamp', String, nullable=False) + + def __repr__(self): + return self.uuid \ No newline at end of file diff --git a/API/DataObjects.pyc b/API/DataObjects.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d2b98d620a89ee456126a93b03f0324378a2c818 GIT binary patch literal 2195 zcmb_dUvJ|?5Fa~#8mCPf4h|6T;J3>Q6$lBT3L$7KL?v`5IlTu=PS(brG`{ZIIq&+8 zNW2{%f^Wcg;xq8Z2Y{JzoTm4HsKANGJF~ODoqs#q`ExY<<5@OOVf%FG`!&7xHzFDR zJEUxncr#%+;iJX_ZHibK&;br0Q8Y>acT$LBsGCa!*Rc#!v_*R$7 zO*ta&h)9K6xx6gwDqoAhqP4^ubwe-%(#UX$RfSn&#)l+Pj*u5MSz^?BjaibGT4TyE zjnYJxrCL>l{SuRfve?WBAo<{OgRHG1|sYO4k6nK4@F4&`@$m;4(N?L(gWzg~;hnXQY*-f*^~x%(H#~k(W0+@HxO|yoC2My}Yl~HQc@dV&&=Hcb zZk5|GV>1@0%i`LGuH;o*DXz}&R5oq7c4>w$^Ay8ZD=Y|#5DY3?S7>PC@RcGEDyc$n z2y-8(W$%xq(zK=kZ5oSOuEp6mfcCF&T~-dWaIq0z8-v*(yGCqdDdQI5-}|&emC2X<q&AiQ*FkiGfCv7 zi*4iAsBA+bt9O2-+!NdPoW_>B-_HQgfm$a|JQf=95o=lx8*WiAr}PCO_A?rtpU$7s z8L;z=Dpk)f(B8OmJ)dEb(-|Z-zb@u{CZ8;xQ;)MJ>y6lzBvemTl00HqI>Kbm^lhTs z$wC=S630zENlcm5nprk7NwPAfF*kJjaF`n9h|P|qGqbH%RgGeP)3%ki7;$5bxI0D< znDBnlJ4_xjd6&t1Ox|bm0g+p0|C7fNUlp8i%gFt4-|zbmy*GaT<0)GuKHC>s>2!r= z6OjlS*EmYzU&Q{2A5L1}PWhy)OvG1O#F2^yL>#+u3&$EKxA%>WxTxOL!JphohP(SG q;w(gLzEtlS-A3Zj{}dd__?JzNzv3KPc532Hy^p+!H}NNa;QtNK`66Th literal 0 HcmV?d00001 diff --git a/API/__pycache__/DataObjects.cpython-36.pyc b/API/__pycache__/DataObjects.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9c5ec1d86cf4e36a6cc8b2e2ce03b7b2cf212166 GIT binary patch literal 1914 zcmb7F&2HO95GE-~B56vNoun<29C!ab2d){Yii;T!OAT=Y>wQ%c}D<&A47x?PoIQ#jsU{8YC~$JTvojQH2JKL7?~* zF7gD-Kx+`V#RFcTg=vYqD85*Lh?87ONN6bz4+JEVYqazR;wd+PeV#y0MXDenn$6Qj ztpggTF=rqF{q|5bbPGh$!5k z9-`g`^%3WpT&j-Hq1 zYI&h~a)rbAd(s5T93b+`G8G5De=&#X#@a_FDl?3hHeAXj>DMJhser6VXDR4Q9g?bv{b`lo|*__%%dsDJi9bek<%(V&!QA?P;c6xW~qQTX#&yPIb>)GEhylo z)Sz(V=q*Phc=lOs2W@T}YT)fH@gylRK+6@Q1~qoJ3E=)37P&4!MAK#M>xwWDG7Eqv zmbKnk`2Rl5feWmI6_GW#y%jFEBix|d+o9qx1y_p|AA%9bQ<$g8{67vS`r+jKboBLC zpMMd(yf}On_*GA?Q#Gc1s9Z|Ai=oYS7CKc}@9`BZs}^=X!%iLH zhh2Hi`Bd#X$W?b8CmdP(UHKd4iH&7U5B*a+#Afiuw0^c;TUbZ-i=nLti19{n4anK= zmcEbh@5$l8cC&X7oLm zSJZi?^VfIcOsF1Drv%M-v8=o}#zfL_{D}Bq?IOE(_nHc%$>V0mi{U;^&$xm( zu7WsLxhN&+VH{tVT&_pDaV+vAj#)2`3yR;WU$@Y5ujlAZw`+?Uh+m0of{M2r((Qy@9U7v^k|m*6h8qqyDh(JH_5rm a`(_p5Zz*96xf start, LocationData.timestamp < end).all() + + dataPoints = defaultdict(list) + for result in query_results: + dataPoints[result.uid].append(result) + + result = [] + for user in dataPoints: + print(user) + json_entry = json.loads('{}') + json_entry['user_id'] = user + json_entry['start_time'] = min(dataPoints[user], key=lambda x:x.timestamp).timestamp + json_entry['end_time'] = max(dataPoints[user], key=lambda x:x.timestamp).timestamp + json_entry['locations'] = [] + for location in dataPoints[user]: + json_location = json.loads('{}') + json_location['latitude'] = location.latitude + json_location['longitude'] = location.longitude + json_location['timestamp'] = location.timestamp + json_entry['locations'].append(json_location) + result.append(json_entry) + + return json.dumps(result) + except Exception as e: + print(e) + return json.dumps([[l.uuid, l.latitude, l.longitude, l.timestamp] for l in session.query(LocationData).all()]) + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/API/config.ini b/API/config.ini new file mode 100644 index 0000000..94db58d --- /dev/null +++ b/API/config.ini @@ -0,0 +1,7 @@ +[database] +type = sqlite +host = C:\Users\demel\Documents\GitHub\GPS-Tracking-of-On-Campus-Walking-Project\API\db\test.db +username = mason +pass = mason +port = 3306 +database = mason \ No newline at end of file diff --git a/API/db/init_sqlite.sql b/API/db/init_sqlite.sql new file mode 100644 index 0000000..7e946da --- /dev/null +++ b/API/db/init_sqlite.sql @@ -0,0 +1,14 @@ +BEGIN TRANSACTION; +DROP TABLE IF EXISTS `location_data`; +CREATE TABLE IF NOT EXISTS `location_data` ( + `uuid` TEXT NOT NULL PRIMARY KEY UNIQUE, + `uid` TEXT NOT NULL, + `latitude` REAL NOT NULL, + `longitude` REAL NOT NULL, + `accuracy` REAL NOT NULL, + `speed` REAL NOT NULL, + `heading` REAL NOT NULL, + `altitude` REAL NOT NULL, + `timestamp` TEXT NOT NULL +); +COMMIT; \ No newline at end of file diff --git a/API/db/test.db b/API/db/test.db new file mode 100644 index 0000000000000000000000000000000000000000..759bb0b12072deac94e806d0bee0c140298378b7 GIT binary patch literal 12288 zcmeI0+in|07{}Lkn#5`B<*qlq*t-t!Qd8-J@`Rn0T>=Xoc8mNmQMtUJzsyGjW6fdCKy z0zd!=00AHX1b_e#00KbZ|3yGIw{P4%Xk?Q!AHKQxdiw3d#?Fm{gT{9kb7QArI&)?| znVp?@GdF9E=EukVqhY@o9(^?E7vr_QadE4&*B&nx6F+VjDTe(ohQ+;4QyC2g#b?KN z?j9XKC_d>wC`R}0+#mJdU&@oZ5~o#e14EWuj#P9yV#%>ll-z#yeCBnp6cddwMT8ZE z^(gI8ekgDim%A%#tv%OKi{!;9;vq()9EpfHW`q+)P(|L}S{H=$s7~&OOjYJjc3xS3 zP)bD&BNWAw$z&}8QbrTxG?r2m>m=oGV_hO#9Yf4}jPy!=NGN;wWP4>yu;9`=ftU?0 z854<=#44SXj)7z8#0HJ8t&D}$+Lc@)Z&*OwM~RF~Cl@HOmUo-;MNG@I z!A!{!=V_}cMFeuT^oko->bODU*2xZ76cFg0zd!=00AHX1b_e#00KY&2mpb9jliwe zs8J`G)vknfQct^;uuh_ArxMml>}*%UI*FKT%Q@CaoNQIXI*E=}C9IQ}Xdbji?K+8s FzW_!Mz9j$v literal 0 HcmV?d00001 diff --git a/API/post_test.py b/API/post_test.py new file mode 100644 index 0000000..c2e3836 --- /dev/null +++ b/API/post_test.py @@ -0,0 +1,44 @@ +import json +import urllib2 +import uuid +import datetime + +data = { + "location": { + "coords": { + "latitude": 1.5, + "longitude": 1.5, + "accuracy": 1.5, + "speed": 1.5, + "heading": 1.5, + "altitude": 1.5 + }, + "extras": { + "foo": "bar" + }, + "activity": { + "type": "still", + "confidence": 100 + }, + "geofence": { + "identifier": "identifier", + "action": "ENTER" + }, + "battery": { + "level": 1.5, + "is_charging": True + }, + "timestamp": str(datetime.datetime.now().replace(microsecond=0).isoformat()), + "uuid": str(uuid.uuid4()), + "event": "EVENT", + "is_moving": 1.0, + "odometer": 1.0 + } + } + +req = urllib2.Request('http://127.0.0.1:5000/api/locationdata') +req.add_header('Content-Type', 'application/json') + +response = urllib2.urlopen(req, json.dumps(data)) + +print(response.read()) \ No newline at end of file