# -*- coding: utf-8 -*-
import os
import serial
import sys
import urllib2
import urllib
def getCPUtemperature():
    res=os.popen('vcgencmd measure_temp').readline()
    return(res.replace("temp=","").replace("C\n",""))
def getRAMinfo():
    p =os.popen('free')
    i=0
    while 1:
          i=i+1
          line = p.readline()
          if i==2:
             return(line.split()[1:4])
def getCPUuse():
    return(str(os.popen("top -n1 | awk /Cpu(/s\):{print $2}").readline().strip()))
def getDiskSpace():
    p=os.popen("df -h /")
    i=0
    line=p.readline()
    if i==2:
       return(line.split()[1:5])
CPU_temp =getCPUtemperature()
CPU_usage=getCPUuse()
RAM_stats=getRAMinfo()
RAM_total=round(int(RAM_stats[0])/1000,1)
RAM_used=round(int(RAM_stats[1])/1000,1)
RAM_free=round(int(RAM_stats[2])/1000,1)
#室内温度数据进行JSON格式的处理
wendu='{"value":%s}' % str(CPU_temp)[0:4]
shiyongneicun='{"value":%s}'% str(RAM_used)
shengyuneicun='{"value":%s}'% str(RAM_free)
output=open('/home/pi/xiangmu/shuju/shumeipaiwendu.txt','w')
out=open('/home/pi/xiangmu/shuju/shiyongneicun.txt','w')
put=open('/home/pi/xiangmu/shuju/shengyuneicun.txt','w')
output.write(wendu)
out.write(shiyongneicun)
put.write(shengyuneicun)
output.close()
out.close()
put.close()
#串口数据接受
ser=serial.Serial('/dev/ttyAMA0',9600,timeout=1)
ser.open()
a=ser.readline()
ser.close()
shineiwendu='{"value":%s}'% a[0:3]#转换成json格式
yanwu='{"value":%s}'% a[4:7]
yan=open('/home/pi/xiangmu/shuju/yanwu.txt','w')
shinei=open('/home/pi/xiangmu/shuju/shineiwendu.txt','w')
yan.write(yanwu)
shinei.write(shineiwendu)
yan.close()
shinei.close()
#gps数据处理
'''gps=open('/home/pi/xiangmu/shuju/gps.txt','r')
Gps=gps.read()
gps.close()
str(Gps)
#print Gps
#print  Gps[0:6],Gps[8:14],Gps[16:20] 
#gps1='{"value":{"lat":%s,"lng":%s,"speed":%s}}' % str(Gps[0:6]),str(Gps[8:14]),str(Gps[16:20])
'''

if __name__=='__main__':
 print('CPU温度 ='+CPU_temp[0:3])
 print('总内存='+str(RAM_total)+'MB')
 print('使用内存='+str(RAM_used)+'MB')
 print('剩余内存='+str(RAM_free)+'MB')
 print('室内温度='+a[4:+6])
 print('室内烟雾浓度＝'+a[0:3])
 s='当前系统温度为'+CPU_temp+'度  '+'室内温度为'+a[4:6]+'度'+'室内烟雾浓度为'+a[0:3]+'使用内存为'+str(RAM_used)+'MB '+'剩余内存为'+str(RAM_free)+'MB'
 zong=open('/home/pi/xiangmu/shuju/zongshuju.txt','w')
 zong.write(s)
 zong.close()
 
 url = "http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&text="+'当前系统温度为'+CPU_temp+'度'+'使用内存为'+str(RAM_used)+'照   '+'剩余内存为'+str(RAM_free)+'照'+'室内温度为'+a[4:6]+'度'+'室内烟雾浓度为'+a[0:3]
  
 wp = urllib.urlopen(url) 
 content = wp.read()
 fp = open("/home/pi/xiangmu/shuju/shuju.mp3","wb")
 fp.write(content)
 os.system("mplayer /home/pi/xiangmu/shuju/shuju.mp3")
 fp.close()
 os.system("curl --request POST --data-binary @'/home/pi/xiangmu/shuju/shineiwendu.txt' --header 'U-ApiKey:150e5309311dac2cd3163d7aa06714ef' http://api.yeelink.net/v1.0/device/340883/sensor/377803/datapoints")#室内温度
 os.system("curl --request POST --data-binary @'/home/pi/xiangmu/shuju/yanwu.txt' --header 'U-ApiKey:150e5309311dac2cd3163d7aa06714ef' http://api.yeelink.net/v1.0/device/340883/sensor/380233/datapoints")#烟雾
 os.system("curl --request POST --data-binary @'/home/pi/xiangmu/shuju/shumeipaiwendu.txt' --header 'U-ApiKey:150e5309311dac2cd3163d7aa06714ef' http://api.yeelink.net/v1.0/device/340883/sensor/380234/datapoints")#机器人温度
 os.system("curl --request POST --data-binary @'/home/pi/xiangmu/shuju/shengyuneicun.txt' --header 'U-ApiKey:150e5309311dac2cd3163d7aa06714ef' http://api.yeelink.net/v1.0/device/340883/sensor/380235/datapoints")#剩余内存
 os.system("curl --request POST --data-binary @'/home/pi/xiangmu/shuju/shiyongneicun.txt' --header 'U-ApiKey:150e5309311dac2cd3163d7aa06714ef' http://api.yeelink.net/v1.0/device/340883/sensor/384587/datapoints")#使用内存
 os.system('sudo fswebcam -d /dev/video0 -r 320*240 --bottom-banner --title "图灵管家@ Home" --no-timestamp /home/pi/xiangmu/shuju/yeelink.jpg')
 os.system('curl --request POST --data-binary @"/home/pi/xiangmu/shuju/yeelink.jpg" --header "U-ApiKey:150e5309311dac2cd3163d7aa06714ef" http://api.yeelink.net/v1.0/device/340392/sensor/376985/photos')#照片上传
 os.system('curl --request POST --data-binary @"/home/pi/xiangmu/shuju/gps.txt" --header "U-ApiKey:150e5309311dac2cd3163d7aa06714ef" http://api.yeelink.net/v1.0/device/340966/sensor/377923/datapoints')#上传地址

