#!/usr/bin/env python

#--- selection of import based on the package ---
''' 1. with python3-serial'''
import serial
''' 2. without python3-serial'''
#from dummySerial import CDummySerial
#serial = CDummySerial()
#-----------------

'''
v0.8 2016 Mar. 1
  - add comm_check()
  - add append_rcvdtext()
v0.7 2016 Feb. 29
  - add comm_bye()
  - add comm_hello()
v0.6 2016 Feb. 29
  - add comm_post()
  - add read_sendtext()
v0.5 2016 Feb. 29
  - add read_name()
  - add debug_outputDebugString()
v0.4 2016 Feb. 24
  - add contact by Vital
v0.3 2016 Feb. 24
  - group_run commands
      + hello
      + check
      + get
      + post
v0.2 2016 Feb. 24
  - add serial print
v0.1 2016 Feb. 24
  - just hello
'''

import time
import os.path
usleep = lambda x: time.sleep(x/1000000.0)

con1 = serial.Serial('/dev/ttyAMA0', 115200, timeout=0.1)

cmdlines = [
    "hello,1,7of9",
    "check",
    "get",
    "post,Vital,hello_Vital,0",
    "hello,2,Vital",
    "check",
    "get",
    "post,7of9,hello_7of9,0"
    ]

def debug_outputDebugString(prfx, msg):
    print "[DEBUG]" + prfx + "," + msg

def read_sendtext():
#    debug_outputDebugString("read_sendtext","Line52 > start")
    srcpath="/home/pi/BYOP/send.txt"

    if os.path.isfile(srcpath) == False:
#        debug_outputDebugString("read_sendtext","Line55 > send.txt not found");
        return
    rdfd = open(srcpath)
    lines = rdfd.readlines()
    rdfd.close()

    return lines

def read_name():
    debug_outputDebugString("read_name","Line49 > start")
    srcpath="/home/pi/BYOP/name.txt"

    if os.path.isfile(srcpath) == False:
        debug_outputDebugString("read_name","Line53 > name.txt not found");
        return
    with open(srcpath,"r") as nmfd:
        mynm = nmfd.read()
        debug_outputDebugString("read_name","Line63 > name:" + mynm)
    return mynm

def append_rcvdtext(appends):
#    debug_outputDebugString("append_rcvdtext","Line82 > start")
    srcpath="/home/pi/BYOP/rcvd.txt"

    debug_outputDebugString("append_rcvdtext","Line85 > " + appends)

    orglines = ""
    if os.path.isfile(srcpath):
        rdfd = open(srcpath)
        orglines = rdfd.readlines()
        rdfd.close()

    wrfd = open(srcpath, "w")
    wrfd.writelines(orglines)
    wrfd.writelines(appends)
    wrfd.close()

#    debug_outputDebugString("append_rcvdtext","Line101 > fin")
    

def comm_post(sends, dstcom):
    for line in sends:
        if "//" in line:
            continue
        msg="post," + line
        print msg,
        dstcom.write(msg)
        rcvd = dstcom.readline()
        time.sleep(5.0) # second

def comm_check(dstcom):
    debug_outputDebugString("comm_check","Line112 > start")
    cmd="check\n"
    dstcom.write(cmd)
    rcvd = dstcom.readline()
    debug_outputDebugString("comm_check","Line119 >" + rcvd)
    if len(rcvd) == 0:
        return 0
    # TODO: 0a > extractCsvRow() 

    
def comm_hello(name, dstcom):
    msg="hello,1," + name # TODO: 0m > serial number > not 1
    debug_outputDebugString("comm_hello",msg)
    dstcom.write(msg)
    rcvd = dstcom.readline() # TODO: 0m > check when ESP8266 is not connected
    time.sleep(5.0) # second

def comm_bye(dstcom):
    msg="bye\n"
    debug_outputDebugString("comm_bye",msg)
    print msg
    dstcom.write(msg)
    rcvd = dstcom.readline()
    time.sleep(5.0) # second
    
def main():
    # hello
    myname = read_name()
    comm_hello(myname, con1)

    # message receive
    comm_check(con1)
    tomsg = "1stline\n"
    tomsg = tomsg + "2ndline\n"
    tomsg = tomsg + "3rdline\n"    
    append_rcvdtext(tomsg)

    # message post
    sends = read_sendtext()
    comm_post(sends,con1)

    # bye
    comm_bye(con1)

'''    
    for idx in range (8):
        msg = cmdlines[idx] + "\n"
        print msg,
        con1.write(msg)
        time.sleep(5.0); # second
'''        

if __name__ == '__main__':
    main()
    con1.close()   

@@ -247,16 +247,69 @@
  Mar. 1%0A
+  - add Test_extractCsvRow()%0A  - add extractCsvRow()%0A
   - add 
@@ -3372,24 +3372,359 @@
 ond%0A
-    %0Adef main():
+%0Adef extractCsvRow(csvline, getIdx):%0A    lines = csvline.split(%22,%22)%0A    if getIdx %3E= len(lines):%0A        return %22%22%0A    return lines%5BgetIdx%5D%0A%0Adef Test_extractCsvRow():%0A    csvline=%22AAA,BBB,CCC%22%0A    print extractCsvRow(csvline, 0)%0A    print extractCsvRow(csvline, 2)%0A    print extractCsvRow(csvline, 4)    %0A    %0Adef main():%0A    Test_extractCsvRow()%0A    
 %0A   

add extractCsvRow()