#!/usr/bin/python
#
# Copyright (c) 2019 Clementine Computing LLC.
#
# This file is part of PopuFare.
#
# PopuFare is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PopuFare 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
#

import os
import serial
import sys
import re
import time
from os import listdir
from os.path import isfile, join
from os import system

GPS_BAUD = 115200
FREQ_SECOND = 3
BITMASK = 31

DEVGPS = "/dev/ttyGPS"

DEBUG = True
VERBOSE = True

###
###

ser = serial.Serial(DEVGPS, GPS_BAUD, bytesize=8, parity='N', stopbits=1, timeout=1, xonxoff=0, rtscts=0)

###
###


if DEBUG: print "send:", "AT+CGPS=1"

ser.write("AT+CGPS=1\r")
ser.flush()
resp = ser.readline()
while (len(resp) > 0) and (resp[0] == '$'):
  resp = ser.readline()
if not  re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
resp = ser.readline()
while (len(resp) > 0) and (resp[0] == '$'):
  resp = ser.readline()
if not  re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"

if DEBUG: print "got:", resp

time.sleep(1)

###
###

if DEBUG: print "send:", "AT+CGPSINFOCFG=" + str(FREQ_SECOND) + "," + str(BITMASK) 

ser.write("AT+CGPSINFOCFG=" + str(FREQ_SECOND) + "," + str(BITMASK) + "\r")
ser.flush()
resp = ser.readline()
while (len(resp) > 0) and (resp[0] == '$'):
  resp = ser.readline()
if not  re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
resp = ser.readline()
while (len(resp) > 0) and (resp[0] == '$'):
  resp = ser.readline()
if not  re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"

if DEBUG: print "got:", resp

time.sleep(1)

if DEBUG: print "closing connection"
ser.close()

sys.exit(0);

###
###

#if DEBUG: print "send:", "AT"
#
#ser.write("\rAT\r")
#ser.flush()
#
#resp = ser.readline()
#if not re.search(r'[aA][tT]', resp):
#  print "ERROR: got response: '" + str(resp) + "', expecting 'OK', exiting"
#  sys.exit(-1)
#
#resp = ser.readline()
#if not re.search(r'[oO][kK]', resp):
#  print "ERROR: got response: '" + str(resp) + "', expecting 'OK', exiting"
#  sys.exit(-1)
#
#if DEBUG: print "got:", resp
#
###
###

if DEBUG: print "send:", "AT+CGPSINFOCFG=0"

ser.write("AT+CGPSINFOCFG=0\r")
ser.flush()
resp = ser.readline()
while (len(resp) > 0) and (resp[0] == '$'):
  resp = ser.readline()
if not  re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
resp = ser.readline()
if not  re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"

if DEBUG: print "got:", resp

time.sleep(1)

###
###

if DEBUG: print "send:", "AT+CGPS=0"

ser.write("AT+CGPS=0\r")
ser.flush()

resp = ser.readline()
if not  re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
resp = ser.readline()
if not  re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"

if DEBUG: print "got:", resp

time.sleep(2)

###
###

if DEBUG: print "send:", "AT+CGPS=1"

ser.write("AT+CGPS=1\r")
ser.flush()
resp = ser.readline()
if not  re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
resp = ser.readline()
if not  re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"

if DEBUG: print "got:", resp

time.sleep(1)

###
###

if DEBUG: print "send:", "AT+CGPSINFOCFG=" + str(FREQ_SECOND) + "," + str(BITMASK) 

ser.write("AT+CGPSINFOCFG=" + str(FREQ_SECOND) + "," + str(BITMASK) + "\r")
ser.flush()
resp = ser.readline()
if not  re.search(r'[aA][tT]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"
resp = ser.readline()
if not  re.search(r'[oO][kK]', resp): print "WARNING: got response: '" + str(resp) + "', expecting 'OK'. continuing"

if DEBUG: print "got:", resp

time.sleep(1)

###
###

if DEBUG: print "closing connection"
ser.close()
