Browse Source

update

yigit
Efe Aydın 6 years ago
parent
commit
c317f74a27
6 changed files with 329 additions and 117 deletions
  1. +35
    -0
      MyCity/app/src/main/res/layout/fragment_crash.xml
  2. +2
    -2
      server_side/apia/app.py
  3. +59
    -0
      server_side/apia/modules/car_crash.py
  4. +0
    -0
      server_side/apia/modules/databases/crashes.json
  5. +2
    -115
      server_side/apia/modules/databases/users.json
  6. +231
    -0
      telnet/telnet.ino

+ 35
- 0
MyCity/app/src/main/res/layout/fragment_crash.xml View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DenunciationFragment" android:orientation="vertical" android:layout_marginTop="20dp">
<!-- TODO: Update blank fragment layout -->
<TextView
android:text="Crash Information"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/announcement_title" android:textSize="24sp"
android:textColor="#000000" android:layout_marginBottom="15dp" android:layout_marginLeft="7dp"/>
<EditText
android:layout_width="match_par yent"
android:layout_height="221dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/denunciation_text"
android:layout_marginTop="8dp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp">
<ImageButton
android:src="@drawable/ic_menu_camera"
android:layout_width="153dp"
android:layout_height="wrap_content" android:id="@+id/denunciation_photo" android:layout_weight="1"/>
<Button
android:text="Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/denunciation_submit" android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

+ 2
- 2
server_side/apia/app.py View File

@ -27,7 +27,7 @@ if __name__ == '__main__':
api.add_resource(user_info.Login, '/login', '/login/')
api.add_resource(user_info.ReducePoints, '/reduce', '/reduce/')
api.add_resource(utility.Resources, '/resources', '/resources/')
# api.add_resource(utility.Resources, '/resources', '/resources/')
api.add_resource(denunciation.Alert, '/denunciation', '/denunciation/')
api.add_resource(denunciation.Denunciations, '/denunciations', '/denunciations/')
@ -38,6 +38,6 @@ if __name__ == '__main__':
api.add_resource(announcements.Announcement, '/announcements', '/announcements/')
#api.add_resource(smart_park.Empty, '/parking', '/parking/')
# api.add_resource(smart_park.Empty, '/parking', '/parking/')
app.run(host='0.0.0.0', port=5000, ssl_context=context)

+ 59
- 0
server_side/apia/modules/car_crash.py View File

@ -0,0 +1,59 @@
from modules import utils
from flask import Flask, request, Response
from flask_restful import Resource, Api
import json
import os
app = Flask(__name__)
api = Api(app)
db_path = os.path.join(app.root_path, 'databases', 'crashes.json')
with open(db_path, 'r') as f:
crashes = json.load(f)
users_path = os.path.join(app.root_path, 'databases', 'users.json')
with open(users_path, 'r') as f:
users = json.load(f)
class Crash(Resource):
def post(self):
args = request.form
reporter = args['id']
user = utils.find_by_id(users.values(), reporter)
trust = int(user["trustability"])
if args["accepted"] == "true" or trust > 20:
photo = args["photo"]
if utils.find_by_id(users.values(), reporter):
denunciation_info = args['note']
denunciation_priority = 5
denunciation_location = {
"latitude": float(args['latitude']),
"longitude": float(args['longitude'])
}
denunciation = {
'id': len(crashes) + 1,
'reporter': reporter,
'emergency': args['emergency'],
'info': denunciation_info,
'photo': photo,
'plates': args.get('plates'),
'injuries': args.get('injuries'),
'lines_blocked': args.get('lines_blocked'),
'priority': denunciation_priority,
'location': denunciation_location
}
crashes.append(denunciation)
with open(db_path, 'w') as f:
json.dump(crashes, f, indent=4)
return {'success': True}
else:
return {'error': 'User doesn\'t exists'}
else:
return {"success": False, "penalty": "{}".format(100*(20-trust))}

server_side/apia/__init__.py~f939ad8155badff39792aaabe628f74142004970 → server_side/apia/modules/databases/crashes.json View File


+ 2
- 115
server_side/apia/modules/databases/users.json View File

@ -7,121 +7,8 @@
"email": "efeaydin@mycity.com",
"TC": "11111111111",
"tel": "0555 555 55 55",
"health": [
"antibiotic allergy",
"peanut allergy",
"asthma"
],
"stats": {
"bus_usage_week": 1,
"bus_usage_month": 5,
"bus_usage_year": 67
},
"plate": "06 EFE 23",
"trustability": "16",
"daily_electricity_usage": [
32,
34,
29,
28,
35,
31,
36,
38,
37,
40,
45,
47,
45,
47,
48,
44,
49,
53,
55,
49,
46,
42,
37,
34
],
"daily_water_usage": [
32,
34,
29,
28,
35,
31,
36,
38,
37,
40,
45,
47,
45,
47,
48,
44,
49,
53,
55,
49,
46,
42,
37,
34
],
"ideal_electricity_usage": [
11,
11,
11,
11,
11,
12,
14,
15,
15,
15,
15,
15,
15,
15,
15,
15,
15,
16,
16,
15,
15,
14,
12
],
"ideal_water_usage": [
11,
11,
11,
11,
11,
12,
14,
15,
15,
15,
15,
15,
15,
15,
15,
15,
15,
16,
16,
15,
15,
14,
12
],
"points": 35,
"electricity_bill": 100,
"water_bill": 120
"points": 35
}
}

+ 231
- 0
telnet/telnet.ino View File

@ -0,0 +1,231 @@
/*
WiFiTelnetToSerial - Example Transparent UART to Telnet Server for esp8266
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the ESP8266WiFi library for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ESP8266WiFi.h>
#include <algorithm> // std::min
#ifndef STASSID
#define STASSID "ZyXEL5500bT5"
#define STAPSK "eBusgxczl5"
#endif
/*
SWAP_PINS:
0: use Serial1 for logging (legacy example)
1: configure Hardware Serial port on RX:GPIO13 TX:GPIO15
and use SoftwareSerial for logging on
standard Serial pins RX:GPIO3 and TX:GPIO1
*/
#define SWAP_PINS 0
/*
SERIAL_LOOPBACK
0: normal serial operations
1: RX-TX are internally connected (loopback)
*/
#define SERIAL_LOOPBACK 0
#define BAUD_SERIAL 115200
#define BAUD_LOGGER 115200
#define RXBUFFERSIZE 1024
#define ONE 12
#define TWO 14
////////////////////////////////////////////////////////////
#if SERIAL_LOOPBACK
#undef BAUD_SERIAL
#define BAUD_SERIAL 3000000
#include <esp8266_peri.h>
#endif
#if SWAP_PINS
#include <SoftwareSerial.h>
SoftwareSerial* logger = nullptr;
#else
#define logger (&Serial1)
#endif
#define STACK_PROTECTOR 512 // bytes
//how many clients should be able to telnet to this ESP8266
#define MAX_SRV_CLIENTS 2
const char* ssid = STASSID;
const char* password = STAPSK;
const int port = 23;
int led_one = 0;
int led_two = 0;
WiFiServer server(port);
WiFiClient serverClients[MAX_SRV_CLIENTS];
void setup() {
Serial.begin(BAUD_SERIAL);
Serial.setRxBufferSize(RXBUFFERSIZE);
#if SWAP_PINS
Serial.swap();
// Hardware serial is now on RX:GPIO13 TX:GPIO15
// use SoftwareSerial on regular RX(3)/TX(1) for logging
logger = new SoftwareSerial(3, 1);
logger->begin(BAUD_LOGGER);
logger->println("\n\nUsing SoftwareSerial for logging");
#else
logger->begin(BAUD_LOGGER);
logger->println("\n\nUsing Serial1 for logging");
#endif
logger->println(ESP.getFullVersion());
logger->printf("Serial baud: %d (8n1: %d KB/s)\n", BAUD_SERIAL, BAUD_SERIAL * 8 / 10 / 1024);
logger->printf("Serial receive buffer size: %d bytes\n", RXBUFFERSIZE);
#if SERIAL_LOOPBACK
USC0(0) |= (1 << UCLBE); // incomplete HardwareSerial API
logger->println("Serial Internal Loopback enabled");
#endif
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
logger->print("\nConnecting to ");
logger->println(ssid);
while (WiFi.status() != WL_CONNECTED) {
logger->print('.');
delay(500);
}
logger->println();
logger->print("connected, address=");
logger->println(WiFi.localIP());
//start server
server.begin();
server.setNoDelay(true);
logger->print("Ready! Use 'telnet ");
logger->print(WiFi.localIP());
logger->printf(" %d' to connect\n", port);
}
void loop() {
//check if there are any new clients
if (server.hasClient()) {
//find free/disconnected spot
int i;
for (i = 0; i < MAX_SRV_CLIENTS; i++)
if (!serverClients[i]) { // equivalent to !serverClients[i].connected()
serverClients[i] = server.available();
logger->print("New client: index ");
logger->print(i);
break;
}
//no free/disconnected spot so reject
if (i == MAX_SRV_CLIENTS) {
server.available().println("busy");
// hints: server.available() is a WiFiClient with short-term scope
// when out of scope, a WiFiClient will
// - flush() - all data will be sent
// - stop() - automatically too
logger->printf("server is busy with %d active connections\n", MAX_SRV_CLIENTS);
}
}
//check TCP clients for data
#if 1
// Incredibly, this code is faster than the bufferred one below - #4620 is needed
// loopback/3000000baud average 348KB/s
int val = 0;
for (int i = 0; i < MAX_SRV_CLIENTS; i++)
while (serverClients[i].available() && Serial.availableForWrite() > 0) {
// working char by char is not very efficient
char telnet_val = serverClients[i].read();
if (telnet_val == 48) { // 0
if (val++ == 0) {
led_one = 0;
} else {
led_two = 0;
}
} else if (telnet_val == 49) { // 1
if (val++ == 0) {
led_one = 1;
} else {
led_two = 1;
}
}
Serial.write(telnet_val);
}
#else
// loopback/3000000baud average: 312KB/s
for (int i = 0; i < MAX_SRV_CLIENTS; i++)
while (serverClients[i].available() && Serial.availableForWrite() > 0) {
size_t maxToSerial = std::min(serverClients[i].available(), Serial.availableForWrite());
maxToSerial = std::min(maxToSerial, (size_t)STACK_PROTECTOR);
uint8_t buf[maxToSerial];
size_t tcp_got = serverClients[i].read(buf, maxToSerial);
size_t serial_sent = Serial.write(buf, tcp_got);
if (serial_sent != maxToSerial) {
logger->printf("len mismatch: available:%zd tcp-read:%zd serial-write:%zd\n", maxToSerial, tcp_got, serial_sent);
}
}
#endif
digitalWrite(ONE, led_one);
digitalWrite(TWO, led_two);
// determine maximum output size "fair TCP use"
// client.availableForWrite() returns 0 when !client.connected()
size_t maxToTcp = 0;
for (int i = 0; i < MAX_SRV_CLIENTS; i++)
if (serverClients[i]) {
size_t afw = serverClients[i].availableForWrite();
if (afw) {
if (!maxToTcp) {
maxToTcp = afw;
} else {
maxToTcp = std::min(maxToTcp, afw);
}
} else {
// warn but ignore congested clients
logger->println("one client is congested");
}
}
//check UART for data
size_t len = std::min((size_t)Serial.available(), maxToTcp);
len = std::min(len, (size_t)STACK_PROTECTOR);
if (len) {
uint8_t sbuf[len];
size_t serial_got = Serial.readBytes(sbuf, len);
// push UART data to all connected telnet clients
for (int i = 0; i < MAX_SRV_CLIENTS; i++)
// if client.availableForWrite() was 0 (congested)
// and increased since then,
// ensure write space is sufficient:
if (serverClients[i].availableForWrite() >= serial_got) {
size_t tcp_sent = serverClients[i].write(sbuf, serial_got);
if (tcp_sent != len) {
logger->printf("len mismatch: available:%zd serial-read:%zd tcp-write:%zd\n", len, serial_got, tcp_sent);
}
}
}
}

Loading…
Cancel
Save