The files contained in this repository can be downloaded to your computer using a svn client.
On Linux you simply type the command displayed below.

This URL has Read-Only access.

Statistics
| Revision:

root / trunk / DomotiGaServer / Pachube.module @ 256

History | View | Annotate | Download (4.8 kB)

1
' Gambas module file
2
3
' Description:
4
' Pachube.module
5
' Support for Pachube (pronounce: patch-bay) sensors network.
6
7
' Development Status:
8
' Just build, so possible bugs around.
9
10
' Links:
11
' http://www.pachube.com
12
13
' DomotiGa - an open source home automation program.
14
' Copyright(C) 2009 Ron Klinkien
15
16
' Read file called COPYING for license details.
17
18
PUBLIC hPost AS NEW HttpClient AS "hPost"
19
PUBLIC tPachube AS Timer
20
21
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22
' start timer
23
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24
PUBLIC SUB Run()
25
26
  ' start poll timer for Pachube
27
  tPachube = NEW Timer AS "tPachube"
28
  tPachube.Delay = Main.iPachubePushTime * 1000 * 60 ' multiply for minutes
29
  tPachube.Start
30
31
END
32
33
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
' gets called at each timer event
35
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36
PUBLIC SUB tPachube_Timer()
37
38
  UploadPachubeData()
39
40
END
41
42
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43
' create xml data and upload it to the service
44
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45
PUBLIC SUB UploadPachubeData()
46
47
  DIM sContent AS String
48
49
  ' if we are already uploading return
50
  IF hPost.Status > 0 THEN
51
    Main.WriteLog(("I'm already uploading EEML data to Pachube, skipping."))
52
    RETURN
53
  END IF
54
55
  ' create xml string
56
  sContent = CreatePachubeData()
57
58
  ' use httpclient to post xml to service
59
  hPost.URL = "https://www.pachube.com/api/feeds/" & Main.iPachubeFeed & ".xml?_method=put&key=" & Main.sPachubeAPIKey
60
  hPost.TimeOut = 10
61
  hPost.Async = TRUE
62
  hPost.Post("text/xml", sContent)
63
64
  IF Main.bPachubeDebug THEN Main.WriteDebugLog("[Pachube] " & sContent)
65
66
END
67
68
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69
' scan pachube devices table and create xml document
70
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71
PRIVATE FUNCTION CreatePachubeData() AS String
72
73
  DIM sXml, sValue, sTag AS String
74
  DIM rResult AS Result
75
  DIM aTags AS String[]
76
77
  ' scan device table
78
  rResult = Main.hDB.Exec("SELECT * FROM devices_pachube")
79
  IF NOT rResult THEN
80
    Main.WriteLog(("Error: table 'devices_pachube' not found!"))
81
    RETURN
82
  END IF
83
84
  ' build header
85
  sXml = "<?xml version='1.0' encoding='UTF-8'?>\n"
86
  sXml &= "<eeml xmlns='http://www.eeml.org/xsd/005' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.eeml.org/xsd/005 http://www.eeml.org/xsd/005/005.xsd'>\n"
87
  sXml &= "<environment>\n"
88
89
  ' create device entries
90
  IF rResult.Count THEN
91
    FOR EACH rResult
92
      sXml &= "<data id='" & rResult!datastreamid & "'>\n"
93
      aTags = Split(rResult!tags, ",")
94
      IF aTags.Count > 0 THEN
95
        FOR EACH sTag IN aTags
96
          sXml &= "<tag>" & LTrim(sTag) & "</tag>\n"
97
        NEXT
98
      ELSE
99
        sXml &= "<tag>" & rResult!tags & "</tag>\n"
100
      END IF
101
      SELECT rResult!value
102
        CASE "Value"
103
          sValue = Devices.GetCurrentValueForDevice(rResult!deviceid)
104
        CASE "Value2"
105
          sValue = Devices.GetCurrentValue2ForDevice(rResult!deviceid)
106
        CASE "Value3"
107
          sValue = Devices.GetCurrentValue3ForDevice(rResult!deviceid)
108
        CASE "Value4"
109
          sValue = Devices.GetCurrentValue3ForDevice(rResult!deviceid)
110
      END SELECT
111
      sXml &= "<value>" & sValue & "</value>\n"
112
      sXml &= "<unit symbol='" & rResult!devicelabel & "' type='" & rResult!unittype & "'>" & rResult!units & "</unit>\n"
113
      sXml &= "</data>\n"
114
    NEXT
115
  END IF
116
  ' close document
117
  sXml &= "</environment>\n"
118
  sXml &= "</eeml>\n"
119
120
  RETURN sXml
121
122
END
123
124
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125
' catch error
126
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127
PUBLIC SUB hPost_Error()
128
129
  Main.WriteDebugLog(("[Pachube] EEML data post error."))
130
131
END
132
133
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134
' check http return code
135
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136
PUBLIC SUB hPost_Finished()
137
138
  DIM iCount AS Integer
139
140
  ' select on http result code and display message
141
  SELECT hPost.Code
142
    CASE 200
143
      Main.WriteLog(("Uploaded EEML data to Pachube."))
144
    CASE 401
145
      Main.WriteLog(("Error authenticating while uploading EEML data to Pachube!"))
146
    CASE 403
147
      Main.WriteLog(("Error forbidden to upload EEML data to Pachube!"))
148
    CASE 404
149
      Main.WriteLog(("Error page not found while uploading EEML data to Pachube!"))
150
    CASE 422
151
      Main.WriteLog(("Error EEML data is not valid after uploading to Pachube!"))
152
    CASE 503
153
      Main.WriteLog(("Error rate limit exceeded while uploading EEML data to Pachube!"))
154
    CASE ELSE
155
      Main.WriteLog(("Unknown error occured while uploading EEML data to Pachube!"))
156
  END SELECT
157
158
  ' if debug is on print all http headers
159
  IF NOT Main.bPachubeDebug THEN RETURN
160
  FOR iCount = 0 TO hPost.Headers.Count - 1
161
    Main.WriteDebugLog("[Pachube] " & Left(hPost.Headers[iCount], Len(hPost.Headers[iCount]) - 1))
162
  NEXT
163
164
END