Advertisement
PlaneteDomo

Exemple VB.Net API Karotz

May 22nd, 2011
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.85 KB | None | 0 0
  1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. ''(C) 2011 - VIALAT Mickaël
  3. ''
  4. '' Boutique en ligne de produit domotique : http://www.planete-domotique.com
  5. ''
  6. '' This code is free software; you can redistribute it and/or
  7. '' modify it under the terms of the GNU Lesser General Public
  8. '' License as published by the Free Software Foundation; either
  9. '' version 2.1 of the License, or (at your option) any later version.
  10. ''
  11. '' This code is distributed in the hope that it will be useful,
  12. '' but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. '' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. '' GNU Lesser General Public License for more details.
  15. ''
  16. '' You should have received a copy of the GNU Lesser General Public
  17. '' License along with this program; if not, write to the Free
  18. '' Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. ''
  20. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  21. Imports System.Web
  22. Imports System.Security.Cryptography
  23. Imports System.Net
  24. Imports System.IO
  25. Imports System.Xml
  26.  
  27. Public Class Karotz
  28.  
  29.     Private _InteractiveID As String
  30.  
  31.     Public Sub New()
  32.         _InteractiveID = ""
  33.  
  34.     End Sub
  35.  
  36.     Private Function getSHA1Hash(ByVal strToHash As String, ByVal strKey As String) As Byte()
  37.         Dim bytesKey() As Byte = System.Text.Encoding.ASCII.GetBytes(strKey)
  38.         Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
  39.         Dim hmacsha1 As New HMACSHA1(bytesKey)
  40.  
  41.         bytesToHash = hmacsha1.ComputeHash((bytesToHash))
  42.  
  43.         Return bytesToHash
  44.     End Function
  45.  
  46.     Private Function getTimeStamp() As Long
  47.         Return CType(DateTime.UtcNow.Subtract(#1/1/1970#).TotalSeconds, Long)
  48.     End Function
  49.  
  50.     Private Function URLEncode(ByVal StringToEncode As String) As String
  51.         Dim TempAns As String
  52.         Dim CurChr As Integer
  53.         CurChr = 1
  54.         TempAns = ""
  55.  
  56.         Do Until CurChr - 1 = Len(StringToEncode)
  57.             Select Case Asc(Mid(StringToEncode, CurChr, 1))
  58.                 Case 48 To 57, 65 To 90, 97 To 122
  59.                     TempAns = TempAns & Mid(StringToEncode, CurChr, 1)
  60.                 Case 32
  61.                     TempAns = TempAns & "%" & Hex(32)
  62.                 Case Else
  63.                     TempAns = TempAns & "%" & Format(Asc(Mid(StringToEncode, CurChr, 1)), "X2")
  64.             End Select
  65.  
  66.             CurChr = CurChr + 1
  67.         Loop
  68.  
  69.         URLEncode = TempAns
  70.     End Function
  71.  
  72.     Sub LoadURL(ByVal url As String)
  73.         Dim client As WebClient = New WebClient()
  74.         Dim data As Stream = client.OpenRead(url)
  75.     End Sub
  76.  
  77.     Public Sub StartKarotz(ByVal apikey As String, ByVal installid As String, ByVal secret As String)
  78.         Dim url As String
  79.         Dim query As String
  80.         Dim r As New Random(System.DateTime.Now.Millisecond)
  81.         Dim urlkarotz As String
  82.  
  83.         urlkarotz = "http://api.karotz.com/api/karotz/start"
  84.  
  85.         query = "apikey=" & apikey & "&installid=" & installid & "&once=" & r.Next(0, 999999999) & "&timestamp=" & getTimeStamp()
  86.  
  87.         url = urlkarotz & "?" & query & "&signature=" & URLEncode(Convert.ToBase64String(getSHA1Hash(query, secret)))
  88.  
  89.         Dim client As WebClient = New WebClient()
  90.         Dim data As Stream = client.OpenRead(url)
  91.         Dim sr As New StreamReader(data)
  92.         Dim Xml As String
  93.         Dim line As String
  94.         Xml = ""
  95.  
  96.         Do
  97.             line = sr.ReadLine()
  98.             Xml = Xml & line
  99.         Loop While line IsNot Nothing
  100.  
  101.         Dim xDoc As XmlDocument = New XmlDocument()
  102.         Dim xNodeList As XmlNodeList
  103.  
  104.         xDoc.LoadXml(Xml)
  105.         xNodeList = xDoc.GetElementsByTagName("interactiveId")
  106.  
  107.         _InteractiveID = xNodeList(0).InnerText
  108.  
  109.     End Sub
  110.  
  111.     '' http://api.karotz.com/api/karotz/interactivemode?action=stop&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  112.     Public Sub StopKarotz()
  113.         Dim sUrl As String
  114.  
  115.         If (_InteractiveID <> "") Then
  116.             sUrl = "http://api.karotz.com/api/karotz/interactivemode?action=stop&interactiveid=" & _InteractiveID
  117.  
  118.             LoadURL(sUrl)
  119.         End If
  120.     End Sub
  121.  
  122.     '' http://api.karotz.com/api/karotz/ears?left=20&right=-30&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  123.     Public Sub actionKarotzEars(ByVal left As Integer, ByVal rigth As Integer)
  124.         Dim sUrl As String
  125.  
  126.         If (_InteractiveID <> "") Then
  127.             sUrl = "http://api.karotz.com/api/karotz/ears?left=" & left & "&right=" & rigth & "&interactiveid=" & _InteractiveID
  128.  
  129.             LoadURL(sUrl)
  130.         End If
  131.     End Sub
  132.  
  133.  
  134.     '' http://api.karotz.com/api/karotz/led?action=pulse&color=77FF44&period=3000&pulse=500&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  135.     Public Sub actionKarotzLedPulse(ByVal color As String, ByVal period As Integer, ByVal pulse As Integer)
  136.         Dim sUrl As String
  137.  
  138.         If (_InteractiveID <> "") Then
  139.             sUrl = "http://api.karotz.com/api/karotz/led?action=pulse&color=" & color & "&period=" & period & "&pulse=" & pulse & "&interactiveid=" & _InteractiveID
  140.  
  141.             LoadURL(sUrl)
  142.         End If
  143.     End Sub
  144.  
  145.     '' http://api.karotz.com/api/karotz/led?action=fade&color=77FF44&period=3000&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  146.     Public Sub actionKarotzLedFade(ByVal color As String, ByVal period As Integer)
  147.         Dim sUrl As String
  148.  
  149.         If (_InteractiveID <> "") Then
  150.             sUrl = "http://api.karotz.com/api/karotz/led?action=fade&color=" & color & "&period=" & period & "&interactiveid=" & _InteractiveID
  151.  
  152.             LoadURL(sUrl)
  153.         End If
  154.     End Sub
  155.  
  156.     '' http://api.karotz.com/api/karotz/led?action=light&color=77FF44&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  157.     Public Sub actionKarotzLedLight(ByVal color As String)
  158.         Dim sUrl As String
  159.  
  160.         If (_InteractiveID <> "") Then
  161.             sUrl = "http://api.karotz.com/api/karotz/led?action=light&color=" & color & "&interactiveid=" & _InteractiveID
  162.  
  163.             LoadURL(sUrl)
  164.         End If
  165.     End Sub
  166.  
  167.     '' http://api.karotz.com/api/karotz/tts?action=speak&lang=EN&text=hello%20world&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  168.     Public Sub actionKarotzTTSSpeak(ByVal txt As String, Optional ByVal lang As String = "FR")
  169.         Dim sUrl As String
  170.  
  171.         If (_InteractiveID <> "") Then
  172.             sUrl = "http://api.karotz.com/api/karotz/tts?action=speak&lang=" & lang & "&text=" & txt & "&interactiveid=" & _InteractiveID
  173.  
  174.             LoadURL(sUrl)
  175.         End If
  176.     End Sub
  177.  
  178.     '' http://api.karotz.com/api/karotz/tts?action=stop&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  179.     Public Sub actionKarotzTTSStop()
  180.         Dim sUrl As String
  181.  
  182.         If (_InteractiveID <> "") Then
  183.             sUrl = "http://api.karotz.com/api/karotz/tts?action=stop&interactiveid=" & _InteractiveID
  184.  
  185.             LoadURL(sUrl)
  186.         End If
  187.     End Sub
  188.  
  189.  
  190.  
  191.     '' http://api.karotz.com/api/karotz/multimedia?action=play&url=http%3A%2F%2Fgoogle.com%2Ftest.mp3&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  192.     Public Sub actionKarotzMultimediaPlay(ByVal url As String)
  193.         Dim sUrl As String
  194.  
  195.         If (_InteractiveID <> "") Then
  196.             sUrl = "http://api.karotz.com/api/karotz/multimedia?action=play&url=" & url & "&interactiveid=" & _InteractiveID
  197.  
  198.             LoadURL(sUrl)
  199.         End If
  200.     End Sub
  201.  
  202.     '' http://api.karotz.com/api/karotz/multimedia?action=pause&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  203.     Public Sub actionKarotzMultimediaPause(ByVal url As String)
  204.         Dim sUrl As String
  205.  
  206.         If (_InteractiveID <> "") Then
  207.             sUrl = "http://api.karotz.com/api/karotz/multimedia?action=pause&interactiveid=" & _InteractiveID
  208.  
  209.             LoadURL(sUrl)
  210.         End If
  211.     End Sub
  212.  
  213.     '' http://api.karotz.com/api/karotz/multimedia?action=resume&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  214.     Public Sub actionKarotzMultimediaResume()
  215.         Dim sUrl As String
  216.  
  217.         If (_InteractiveID <> "") Then
  218.             sUrl = "http://api.karotz.com/api/karotz/multimedia?action=resume&interactiveid=" & _InteractiveID
  219.  
  220.             LoadURL(sUrl)
  221.         End If
  222.     End Sub
  223.  
  224.     '' http://api.karotz.com/api/karotz/multimedia?action=stop&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  225.     Public Sub actionKarotzMultimediaStop()
  226.         Dim sUrl As String
  227.  
  228.         If (_InteractiveID <> "") Then
  229.             sUrl = "http://api.karotz.com/api/karotz/multimedia?action=stop&interactiveid=" & _InteractiveID
  230.  
  231.             LoadURL(sUrl)
  232.         End If
  233.     End Sub
  234.  
  235.     '' http://api.karotz.com/api/karotz/multimedia?action=previous&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  236.     Public Sub actionKarotzMultimediaPrevious()
  237.         Dim sUrl As String
  238.  
  239.         If (_InteractiveID <> "") Then
  240.             sUrl = "http://api.karotz.com/api/karotz/multimedia?action=previous&interactiveid=" & _InteractiveID
  241.  
  242.             LoadURL(sUrl)
  243.         End If
  244.     End Sub
  245.  
  246.     '' http://api.karotz.com/api/karotz/multimedia?action=next&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  247.     Public Sub actionKarotzMultimediaNext()
  248.         Dim sUrl As String
  249.  
  250.         If (_InteractiveID <> "") Then
  251.             sUrl = "http://api.karotz.com/api/karotz/multimedia?action=next&interactiveid=" & _InteractiveID
  252.  
  253.             LoadURL(sUrl)
  254.         End If
  255.     End Sub
  256.  
  257.     '' http://api.karotz.com/api/karotz/webcam?action=photo&url=http://blah&interactiveid=3e910454-a6e4-48b0-bd16-4483360947fc
  258.     Public Sub actionKarotzWebcam(ByVal urlPost As String)
  259.         Dim sUrl As String
  260.  
  261.         If (_InteractiveID <> "") Then
  262.             sUrl = "http://api.karotz.com/api/karotz/webcam?action=photo&url=" & urlPost & "&interactiveid=" & _InteractiveID
  263.  
  264.             LoadURL(sUrl)
  265.         End If
  266.     End Sub
  267.  
  268. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement