MonkeySqueeze Development V3.0.2.

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey.

Moderator: Gurus

MonkeySqueeze Development V3.0.2.

Postby Gingernut63 » Sun Aug 14, 2011 5:56 pm

This topic is for the development of MonkeySqueeze, a script which enables MediaMonkey to play music on a Logitech Squeezebox system via Logitech Media Server (Squeezebox Server).

MonkeySqueeze, originally called Squeezebox Controller, was written in 2007 and over the next 2 years was improved by a number of people on this site. Development tapered off after this point and eventually it was re-posted in June 2011 by mccstumble who had collated all improvements on the forum to have a working version. I then fixed broken code and added new functionality to get it to the point where it was made available on the Addon site late July 2011.

I am the project coordinator of the script, to make sure we have a direction and to keep MonkeySqueeze on track. I am relatively inexperienced in writing VBScript and do not have the expertise at this stage to make major modifications to the code so I am always on the lookout for people to help. If you think you have what it takes to improve MonkeySqueeze just post in this topic.

Gingernut63

Download MonkeySqueeze (official release): http://www.mediamonkey.com/addons/brows ... eysqueeze/
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Squeezebox site: http://forums.slimdevices.com/showthrea ... 0a&t=89046

The MonkeySqueeze Programming Honour Roll: booblers, Katteman

Below is a list of issues that need to be addressed to improve the functionality. This is not the definitive list and if you feel you can add to the script please contribute your ideas.

Major Issues:
>> MonkeySqueeze will not communicate with Logitech Media Server (Squeezebox Server) if password protection is enabled in LMS

Minor Issues:
>> VBScript error 9 "m" out of range, Line 565 ( LMSpaths(m) = LMSpaths(m-1)). It occasionally occurs when adding tracks to current playlist -Drag/Drop or Auto DJ. Hard to replicate
>> When adjusting the rating of a currently playing track in MediaMonkey, the music pauses for a very short time in the Squeezebox system
>> "I find that it does not fully support Unicode. Some of my songs are named in Vietnamese, and when I play them on MediaMonkey, the playlist is not relayed to Touch. If I rename them to, say, A.flac or Try this name.mp3, it works."
>> Limit the number of tracks initially added when activating playlist sync to avoid long delays.

Wishlist:
>> Manual/Auto Start selection on the options page.
>> CDs played in MediaMonkey do not play on the Squeezebox system via MonkeySqueeze. (Will only be able to work with a local installation of LMS e.g. c:\)
>> Connect directly to Tiny Squeezebox Server in the Squeezebox Touch.
>> Control the volume on the Squeezebox device via MonkeySqueeze via remote app?.

Posting Scripts

All beta releases will be issued by myself. Contributors can post individual subroutines of working modifications or if multiple subroutines are modified then the whole script can be posted as an Alpha. Designations for full scripts are: the script being developed for the next release (V2.0.1); plus date Year/Month/Day (110622); and Alpha or Beta e.g. V2.0.1.110622Beta. To keep a linear progression of the script all code posted must include previously posted modifications.

Last 5 Beta Releases:

V2.1.0.111031Beta
-Power toggle for primary Squeezebox device when MonkeySqueeze is enabled
-Bug fix: On seek now works for FLAC files as well as mp3 - maybe others
-Bug fix: The music plays on LMS the first time play is pressed in MediaMonkey after re-enabling MonkeySqueeze (issue only on some installations)
-Bug fix: Clicking the MonkeySqueeze enable/disable button no longer gives the error " Object Required 'SongValue' ".
-Revised the options page

V2.1.0.111109Alpha
-Playlist sync between MM and LMS! Not perfect but a huge advance. Still undergoing development.

V2.1.0.111112Beta
- Fixed: adding tracks to a playing playlist
- Warning popups if shuffle, repeat and crossfade selected when syncing playlists
- Progress bar now indicates that a playlist sync is taking place

V3.0.1.120314Beta
- Modified options page for connection to Windows Home Server 2011 and other Windows server products

Current:
V3.1.0.120627Beta
- Made changes to the code for using the Hostname of the server or the IP Address. Very useful if LMS runs on another computer or network server and the IP Address is not static or constantly changes after a reboot or restart.
Last edited by Gingernut63 on Sat Dec 14, 2013 2:48 pm, edited 29 times in total.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development

Postby booblers » Sun Aug 14, 2011 8:18 pm

deleting my posts since forum moderators see fit to delete honest posts. just helping to make their job easier. nothing to see here.
Last edited by booblers on Mon Jan 09, 2012 11:05 pm, edited 1 time in total.
booblers
 
Posts: 97
Joined: Sat Sep 27, 2008 6:57 pm

Re: MonkeySqueeze Development

Postby Gingernut63 » Sun Aug 14, 2011 8:46 pm

@booblers

Phenomenal! Did you this in the last few hours or have you mucking around with it for a while?

I'll have a play to get familiar with what you have done and I'll post my thoughts. Great work, no make that, fantastic work!
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development

Postby booblers » Sun Aug 14, 2011 10:42 pm

deleting my posts since forum moderators see fit to delete honest posts. just helping to make their job easier. nothing to see here.
Last edited by booblers on Mon Jan 09, 2012 11:05 pm, edited 1 time in total.
booblers
 
Posts: 97
Joined: Sat Sep 27, 2008 6:57 pm

Re: MonkeySqueeze Development

Postby booblers » Sun Aug 14, 2011 11:40 pm

deleting my posts since forum moderators see fit to delete honest posts. just helping to make their job easier. nothing to see here.
Last edited by booblers on Mon Jan 09, 2012 11:05 pm, edited 1 time in total.
booblers
 
Posts: 97
Joined: Sat Sep 27, 2008 6:57 pm

Re: MonkeySqueeze Development

Postby Gingernut63 » Sun Aug 14, 2011 11:54 pm

Don't worry I have some Icons I made myself for this task a while ago. If they don't work I'll PM you with my e-mail address.

I'm converting your changes to the new script. Have done the first part will get to on/off part soon. Have programmed the IP and MAC addresses on the settings page to work with your changes. Testing OK at this stage. When I've added in the on/off section I'll repost the lot so everything is current.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development - Alpha110815

Postby Gingernut63 » Mon Aug 15, 2011 1:54 am

@booblers

Here is the up to date script combining all your changes plus the latest script version which has just been released.

I programmed in the ability to set the IP and MAC address' on the settings page. I've also deleted obsolete code and added in separators between the subroutines to make the code easier read. The last bit's for me. I find it a bit daunting seeing code all over the place.

My icons do not show (modelled on Last.FM icons) and the on/off function is a bit hit and miss for me. There is a couple of other issues but I can't remember what they are at this stage.

Well done!!

Code: Select all
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'
' MonkeySqueeze V2-0-0-110815Alpha
'
' ORIGINAL AUTHOR: Todd Nemeth/revel
' DATE STARTED: 26.09.2007
'
' ADDITIONAL AUTHORS: Baz, Big_Berny, Peke, trixmoto, debacle, mccstumble, Gingernut63, booblers
' UPDATE DATE: XX.XX.20XX
'
' COMMENT:
'  - You will also need Squeezebox Server and SqueezePlay installed and running.
'  - After installation the interface for this script is located at Tools\Options\MonkeySqueeze
'
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Option Explicit
Public SqueezeBoxSocket
Public ManualTrackChange
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim onMonkeySqueezeIcon
Dim offMonkeySqueezeIcon
    'Note - you need to include the port here
'Dim sbIP : sbIP = "XXX.XXX.XXX.XXX:9000"
'You can get this mac address manually and hardcode it as I have here or you could make another JSON call to "status" and get the MAC address.
'it will be required to directly send commands to a particular player - which is what you want to do. It is in every call to the player.
'I changed my address below for security reasons - you will need to input yours below.
'Dim sbMAC: sbMAC = "XX:XX:XX:XX:XX:XX"
'**************************************************************
Sub OnStartup
    Dim ind
    Dim UI : Set UI = SDB.UI
    Dim Mnu
       onMonkeySqueezeIcon = SDB.RegisterIcon("Scripts\Auto\sbon.ico", 0)
    If onMonkeySqueezeIcon < 0 Then
          onMonkeySqueezeIcon = 74 'unlocked icon if it doesn't exist
    End If
       
       offMonkeySqueezeIcon = SDB.RegisterIcon("Scripts\Auto\sboff.ico", 0)
    If offMonkeySqueezeIcon < 0 Then
        offMonkeySqueezeIcon = 75 'locked icon if it doesn't exist
    End If

    Set Mnu = UI.AddMenuItem( UI.Menu_TbStandard, 0, 0)
       Mnu.Caption = "MonkeySqueeze"
         Mnu.UseScript = Script.ScriptPath
         Mnu.OnClickFunc = "SyncToggle"
         Mnu.IconIndex = onMonkeySqueezeIcon
         Mnu.Hint = "MonkeySqueeze"
     
    Set SDB.Objects("MonkeySqueezeIcon") = Mnu      'save a reference to it so we can change the icon later.

    If SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = True Then
    Call registerEvents
       Mnu.IconIndex = onMonkeySqueezeIcon
    Else
        Mnu.IconIndex = offMonkeySqueezeIcon
    End If

       ind = SDB.UI.AddOptionSheet("MonkeySqueeze",Script.ScriptPath,"InitSheet","SaveSheet",-2)
         'InitSqueezeBox
         'ManualTrackChange = True
End Sub
'**************************************************************
Sub InitSheet(Sheet)
     Dim oPanel1, oCheck1, oLabelAbout, oPanel2, Edit1, oPanel3, Btn1, Edit2

     Set oPanel1 = SDB.UI.NewGroupBox(Sheet)
           oPanel1.Common.SetRect 10,10,450,75
           oPanel1.Caption = "MonkeySqueeze v1.5.0.110814"
     With SDB.UI.NewLabel(oPanel1)
           .Common.SetRect 15,20,440,40
           .Caption = "This script allows MediaMonkey to play music on SqueezeBox Server" & vbNewLine _
           & vbNewLine _
           & "To Enable/Disable this addon, go to the Play Menu and click on MonkeySqueeze"
     End With
 
     Set oPanel2 = SDB.UI.NewGroupBox(Sheet)
           oPanel2.Common.SetRect 10,95,450,80
           oPanel2.Caption = "User Guide"
 
     With SDB.UI.NewLabel(oPanel2)
           .Common.SetRect 15,20,440,40
           .Caption = "Click the button and then open MonkeySqueeze_UserGuide.pdf"
     End With
 
     Set Btn1 = SDB.UI.NewButton(oPanel2)
          Btn1.Common.SetRect 175,45,80,25
          Btn1.Caption = "User Guide"
          Btn1.Common.Hint = "Click to open the folder where the user guide is located"
          Script.RegisterEvent Btn1, "OnClick", "UserGuide"
   
     Set oPanel3 = SDB.UI.NewGroupBox(Sheet)
           oPanel3.Common.SetRect 10,185,450,220
           oPanel3.Caption = "Squeezebox Server IP and MAC Address"

     With SDB.UI.NewLabel(oPanel3)
           .Multiline = True
           .Common.SetRect 15,20,420,60
           .Caption = "This script uses the IP address displayed in the information section of Squeezebox Server. Click on the icon in the task bar and open the Control Panel. Under the information tab, find the Server IP Address, e.g. 192.168.0.102 and enter below."
    End With

     With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 105,79,70,20
           .Caption = "IP Address:"
     End With
 
     Set Edit1 = SDB.UI.NewEdit(oPanel3)
           Edit1.Common.ControlName = "SqueezeBoxIP"
           Edit1.Common.SetRect 165,75,95,25
           Edit1.Text = SDB.IniFile.StringValue("SqueezeBox","IP")
      If Edit1.Text = "" Then
           Edit1.Text = "XXX.XXX.XXX.XXX"
           SDB.IniFile.StringValue("SqueezeBox","IP") = "192.168.0.102"
     End If
           Edit1.Common.Hint = "Enter Squeezebox Server IP Address Here"
   
   With SDB.UI.NewLabel(oPanel3)
           .Multiline = True
           .Common.SetRect 15,105,420,60
           .Caption = "The MAC address is obtained from SqueezePlay installed on the same computer as Squeezebox Server. Select Settings > Advanced > Squeezebox Information > SqueezePlay Information and enter address below." 
     End With
      
   With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 90,162,70,20
           .Caption = "MAC Address:"
     End With
 
     Set Edit2 = SDB.UI.NewEdit(oPanel3)
           Edit2.Common.ControlName = "SqueezeBoxMAC"
           Edit2.Common.SetRect 160,157,110,25
           Edit2.Text = SDB.IniFile.StringValue("SqueezeBoxx","MAC")
      If Edit2.Text = "" Then
           Edit2.Text = "XX:XX:XX:XX:XX:XX"
           SDB.IniFile.StringValue("SqueezeBoxx","MAC") = "XX:XX:XX:XX:XX:XX"
     End If
           Edit2.Common.Hint = "Enter Squeezebox Server MAC Address Here"
   
   With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 15,192,420,20
           .Caption = "Press OK and restart MediaMonkey for the changes to take effect."
   End With   
End Sub
'**************************************************************
Sub SaveSheet(Sheet)
        SDB.IniFile.StringValue("SqueezeBox","IP") = Sheet.Common.ChildControl("SqueezeBoxIP").Text
      SDB.IniFile.StringValue("SqueezeBoxx","MAC") = Sheet.Common.ChildControl("SqueezeBoxMAC").Text
End Sub
'**************************************************************
Sub UserGuide
    On Error Resume Next
   Dim OF, FolderToOpen
   Set OF = CreateObject("Shell.Application")
        FolderToOpen = sdb.ScriptsPath & "Auto\"
        OF.Explore FolderToOpen
   Set OF = Nothing
End Sub
'**************************************************************
Sub Event_OnPlay
    Dim strRetVal, TrackPath
         TrackPath = CheckPath(SDB.Player.CurrentSong)
    If ManualTrackChange = True Then
        slimRequest(formatCommand("""stop"""))
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
    Else
        slimRequest(formatCommand("""playlist"", ""add"", """ + perlEscape(TrackPath) + """"))
       ManualTrackChange = True
   End If
End Sub
'**************************************************************
Function SyncToggle( itm )
    Dim TrackPath
    Dim Mnu
       TrackPath = CheckPath(SDB.Player.CurrentSong)
    If SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = False Then
        SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = True
    Call registerEvents
        itm.IconIndex = SDB.RegisterIcon("Scripts\Auto\sbon.ico", 0)
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
        slimRequest(formatCommand("""time"", """ + CStr(SDB.Player.PlaybackTime*0.001 + 1) + """"))      'added 1 second to try to make up for net and lookup lag
    Else
    Call unregisterEvents
        slimRequest(formatCommand("""stop"""))
        SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = False
           itm.IconIndex = SDB.RegisterIcon("Scripts\Auto\sboff.ico", 0)
    End If
End Function
'**************************************************************
Function CheckPath(SongValue)
    If Left(SongValue.Path,1) = "?" Then
    If SongValue.Cached Then
        CheckPath = SongValue.CachedPath
    Else
       CheckPath = SongValue.Path
    End If
    Else
       CheckPath = SongValue.Path
   End If
End Function
'**************************************************************
Sub Event_OnStop
       slimRequest(formatCommand("""stop"""))
End Sub
'**************************************************************
Sub Event_OnShutdown
     On Error Resume Next
       slimRequest(formatCommand("""stop"""))
End Sub
'**************************************************************
Sub Event_OnPause
   If SDB.Player.IsPaused Then
       slimRequest(formatCommand("""pause"""))
    Else
        slimRequest(formatCommand("""play"""))
    End If
End Sub
'**************************************************************
Sub Event_OnSeek
    Dim strRetVal, TrackPath
        strRetVal = SqueezeBoxMode
         TrackPath = CheckPath(SDB.Player.CurrentSong)
    If InStr(1,strRetval,"mode stop") > 0 Then
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
    End If
         slimRequest(formatCommand("""time"", """ + CStr(SDB.Player.PlaybackTime*0.001) + """"))
End Sub
'**************************************************************
Function slimRequest(params)
      'slimRequest accepts pre-formatted parameter string, returns string of the response'
      'logme "  >> slimRequest: Begin with " + params

      'Dim url : url = "http://" + sbIP + "/jsonrpc.js"
   Dim url : url = "http://" + SDB.IniFile.StringValue("SqueezeBox","IP") & ":9000" + "/jsonrpc.js"
   'Dim prog : Set prog = SDB.Objects("LoadXMLBar")
    Dim prog : Set prog = SDB.Progress
    Dim objHTTP : Set objHTTP = CreateObject("Microsoft.XMLHTTP")
    Call objHTTP.open("POST", url, True)
         objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
         objHTTP.setRequestHeader "X-Requested-With", "XMLHttpRequest"
         objHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1"
         objHTTP.send(params)

    Dim cnt : cnt = 0
    While (objHTTP.readyState < 4) And (cnt < 300)
    Call SDB.Tools.Sleep(100)
        SDB.ProcessMessages
        cnt = cnt+1
    If prog.Terminate Then
       cnt = 300
    End If   
    WEnd
    If objHTTP.readyState < 4 Then
    Set slimRequest = Nothing
    Else
    Dim str : str = objHTTP.responseText
          slimRequest = str
      End If
End Function
'*************************************************************
'This is is a JSON RPC call - We're directly calling functions in the Slim::Request module in Perl. See here:
'http://svn.slimdevices.com/repos/slim/7.5/trunk/server/Slim/Control/Request.pm
'the addDispatch section tells you how to use the commands and what the RPC module is looking for in terms of formatting
    Function formatCommand(str1)
    Dim str2
       'str2 = "{""id"":1,""method"":""slim.request"",""params"":[""" + sbMAC + """,[" + str1 + "]]}"
      str2 = "{""id"":1,""method"":""slim.request"",""params"":[""" + SDB.IniFile.StringValue("SqueezeBoxx","MAC") + """,[" + str1 + "]]}"
      formatCommand = str2
End Function
'*************************************************************
'borrowed from a teknojunky - if you want the popups add them to this and then just call logme
Sub logme(msg)
     Dim fso, logf
    On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set logf = fso.OpenTextFile(Script.ScriptPath&".log",ForAppending,True)
       logf.WriteLine Now() & ": " & msg
    Set fso = Nothing
    Set logf = Nothing
End Sub
'*************************************************************
'Perl is taking the JSON strings directly into variables - "\" is a special character in perl to escape the following string
'wherever we have a "\" we need it to be "\\" in anything that goes to Perl (ie: anything that goes to Squeezebox)
Function perlEscape(str1)
         str1 = Replace(str1, "\", "\\")
       perlEscape = str1
End Function
'*************************************************************
Sub registerEvents
    Call Script.RegisterEvent(SDB,"OnPlay","Event_OnPlay")
    Call Script.RegisterEvent(SDB,"OnPause","Event_OnPause")
    Call Script.RegisterEvent(SDB,"OnStop","Event_OnStop")
    Call Script.RegisterEvent(SDB,"OnSeek","Event_OnSeek")
End Sub
'*************************************************************
Sub unregisterEvents
        Script.UnregisterEvents SDB
End Sub
'*************************************************************
'These are what the full JSON strings look like that need to be sent to slimRequest()
'formatRequest() fills in the first part and all you have to do is pick the RPC commands from http://svn.slimdevices.com/repos/slim/7.5/trunk/server/Slim/Control/Request.pm

'Note: there are cleaner ways of doing this - but this is quick and dirty. It just means that you have to use a lot of """ and etc in vbscript.
'This entire script is much easier to write in jscript but I don't know jscript at all so, shrug.

'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["status","-",1,"tags:cgABbehldiqtyrSuoKLN"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["status","-",1,"tags:uB"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["playlist","index",28]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["pause"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",[""playlist"", ""play"", ""D:\\MP3\\filename.mp3""]]}


'You can do ANYTHING you can do from the web interface via this RPC engine because you are directly calling the methods in Perl
'so you could add whole playlists and etc see the docs. Sky's the limit.
Last edited by Gingernut63 on Sun Aug 28, 2011 4:28 pm, edited 1 time in total.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development

Postby Gingernut63 » Mon Aug 15, 2011 5:14 am

@booblers

More info.

Disabling autoplay is fine with me.

My icons are now working fine, will use yours when they are sent.

The on/off is function is misbehaving at the moment. If on/off control was active on shutdown then the icon is green on startup. However when play is activated, music plays on MediaMonkey but does not play on Squeezebox Server. Toggling the button back to green then starts music playing on Squeezebox Server. With music playing and toggling the button red, music stops on SS and continues on MM (good). Stopping MM and toggling green causes music to start playing on SS with MM stopped. Start playing on MM does nothing to SS until MM is stopped and play is pressed, music then plays on both systems

Onseek is now broken. Not a big issue I don't use it myself, will give it low priority at this stage.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development - Beta110816

Postby Gingernut63 » Tue Aug 16, 2011 3:20 pm

For anyone wishing to try the script you do not need the icons as MonkeySqueeze will be displayed on the menu and w3Sockets is no longer required for the comms. FWD and REW (seek) still only work for mp3 files.

Booblers has also provided suggestions on how to indent the code which hopefully I have fixed in this version.

Many thanks to booblers for rewriting the code to remove the reliance on w3Sockets and taking the script to the next level by providing improved features


Code: Select all
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'
' MonkeySqueeze V2-0-0-110816Beta
'
' ORIGINAL AUTHOR: Todd Nemeth/revel
' DATE STARTED: 26.09.2007
'
' ADDITIONAL AUTHORS: Baz, Big_Berny, Peke, trixmoto, debacle, mccstumble, Gingernut63, booblers
' UPDATE DATE: XX.XX.20XX
'
' COMMENT:
'  - You will also need Squeezebox Server and SqueezePlay installed and running.
'  - After installation the interface for this script is located at Tools\Options\MonkeySqueeze
'
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Option Explicit
Public SqueezeBoxSocket
Public ManualTrackChange
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim onMonkeySqueezeIcon
Dim offMonkeySqueezeIcon
    'Note - you need to include the port here
'Dim sbIP : sbIP = "XXX.XXX.XXX.XXX:9000"
'You can get this mac address manually and hardcode it as I have here or you could make another JSON call to "status" and get the MAC address.
'it will be required to directly send commands to a particular player - which is what you want to do. It is in every call to the player.
'I changed my address below for security reasons - you will need to input yours below.
'Dim sbMAC: sbMAC = "XX:XX:XX:XX:XX:XX"
'**************************************************************
Sub OnStartup
     Dim ind
     Dim UI : Set UI = SDB.UI

      Dim Mnu
   
      onMonkeySqueezeIcon = SDB.RegisterIcon("Scripts\Auto\sbon.ico", 0)
      If onMonkeySqueezeIcon < 0 Then
         onMonkeySqueezeIcon = 74 'unlocked icon if it doesn't exist
      End If
   
      offMonkeySqueezeIcon = SDB.RegisterIcon("Scripts\Auto\sboff.ico", 0)
      If offMonkeySqueezeIcon < 0 Then
         offMonkeySqueezeIcon = 75 'locked icon if it doesn't exist
      End If

     Set Mnu = UI.AddMenuItem( UI.Menu_TbStandard, 0, 0)
     Mnu.Caption = "Monkey Squeeze"
     Mnu.UseScript = Script.ScriptPath
     Mnu.OnClickFunc = "SyncToggle"
     Mnu.IconIndex = onMonkeySqueezeIcon
     Mnu.Hint = "Monkey Squeeze"
 
     Set SDB.Objects("MonkeySqueezeIcon") = Mnu      'save a reference to it so we can change the icon later.

     If SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = True Then
         Call registerEvents
         Mnu.IconIndex = onMonkeySqueezeIcon
      Else
         Mnu.IconIndex = offMonkeySqueezeIcon
      End If

     ind = SDB.UI.AddOptionSheet("MonkeySqueeze",Script.ScriptPath,"InitSheet","SaveSheet",-2)
     InitSqueezeBox
     ManualTrackChange = True
     
End Sub

'**************************************************************
Sub InitSheet(Sheet)
     Dim oPanel1, oCheck1, oLabelAbout, oPanel2, Edit1, oPanel3, Btn1, Edit2

     Set oPanel1 = SDB.UI.NewGroupBox(Sheet)
      oPanel1.Common.SetRect 10,10,450,75
      oPanel1.Caption = "MonkeySqueeze v1.5.0.110814"
     With SDB.UI.NewLabel(oPanel1)
           .Common.SetRect 15,20,440,40
           .Caption = "This script allows MediaMonkey to play music on SqueezeBox Server" & vbNewLine _
           & vbNewLine _
           & "To Enable/Disable this addon, go to the Play Menu and click on MonkeySqueeze"
     End With
 
     Set oPanel2 = SDB.UI.NewGroupBox(Sheet)
      oPanel2.Common.SetRect 10,95,450,80
      oPanel2.Caption = "User Guide"
 
     With SDB.UI.NewLabel(oPanel2)
           .Common.SetRect 15,20,440,40
           .Caption = "Click the button and then open MonkeySqueeze_UserGuide.pdf"
     End With
 
     Set Btn1 = SDB.UI.NewButton(oPanel2)
     Btn1.Common.SetRect 175,45,80,25
     Btn1.Caption = "User Guide"
     Btn1.Common.Hint = "Click to open the folder where the user guide is located"
     Script.RegisterEvent Btn1, "OnClick", "UserGuide"
   
     Set oPanel3 = SDB.UI.NewGroupBox(Sheet)
      oPanel3.Common.SetRect 10,185,450,220
      oPanel3.Caption = "Squeezebox Server IP and MAC Address"

     With SDB.UI.NewLabel(oPanel3)
           .Multiline = True
           .Common.SetRect 15,20,420,60
           .Caption = "This script uses the IP address displayed in the information section of Squeezebox Server. Click on the icon in the task bar and open the Control Panel. Under the information tab, find the Server IP                Address, e.g. 192.168.0.102 and enter below."
    End With

     With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 105,79,70,20
           .Caption = "IP Address:"
     End With
 
     Set Edit1 = SDB.UI.NewEdit(oPanel3)
      Edit1.Common.ControlName = "SqueezeBoxIP"
      Edit1.Common.SetRect 165,75,95,25
      Edit1.Text = SDB.IniFile.StringValue("SqueezeBox","IP")
      If Edit1.Text = "" Then
           Edit1.Text = "XXX.XXX.XXX.XXX"
           SDB.IniFile.StringValue("SqueezeBox","IP") = "XXX.XXX.XXX.XXX"
     End If
      Edit1.Common.Hint = "Enter Squeezebox Server IP Address Here"
   
   With SDB.UI.NewLabel(oPanel3)
           .Multiline = True
           .Common.SetRect 15,105,420,60
           .Caption = "The MAC address is obtained from SqueezePlay installed on the same computer as Squeezebox Server. Select Settings > Advanced > Squeezebox Information > SqueezePlay Information and enter address below." 
     End With
      
   With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 90,162,70,20
           .Caption = "MAC Address:"
     End With
 
     Set Edit2 = SDB.UI.NewEdit(oPanel3)
      Edit2.Common.ControlName = "SqueezeBoxMAC"
      Edit2.Common.SetRect 160,157,110,25
      Edit2.Text = SDB.IniFile.StringValue("SqueezeBoxx","MAC")
      If Edit2.Text = "" Then
           Edit2.Text = "XX:XX:XX:XX:XX:XX"
           SDB.IniFile.StringValue("SqueezeBoxx","MAC") = "XX:XX:XX:XX:XX:XX"
     End If
      Edit2.Common.Hint = "Enter Squeezebox Server MAC Address Here"
   
   With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 15,192,420,20
           .Caption = "Press OK and restart MediaMonkey for the changes to take effect."
   End With   
End Sub
'**************************************************************
Sub SaveSheet(Sheet)
   SDB.IniFile.StringValue("SqueezeBox","IP") = Sheet.Common.ChildControl("SqueezeBoxIP").Text
   SDB.IniFile.StringValue("SqueezeBoxx","MAC") = Sheet.Common.ChildControl("SqueezeBoxMAC").Text
End Sub
'**************************************************************
Sub UserGuide
    On Error Resume Next
   Dim OF, FolderToOpen
   Set OF = CreateObject("Shell.Application")
   FolderToOpen = sdb.ScriptsPath & "Auto\"
   OF.Explore FolderToOpen
   Set OF = Nothing
End Sub
'**************************************************************
Sub InitSqueezeBox
     On Error Resume Next
     Set SqueezeBoxSocket = CreateObject("Socket.TCP")
     SqueezeBoxSocket.DoTelnetEmulation = True
     SqueezeBoxSocket.TelnetEmulation = "TTY"
     SqueezeBoxSocket.Host = SDB.IniFile.StringValue("SqueezeBox","IP") & ":9090"
     SqueezeBoxSocket.Open
End Sub
'**************************************************************
Function SqueezeBoxMode
     On Error Resume Next
     SqueezeBoxMode = ""
   'SqueezeBoxSocket.SendLine "mode ?"
   'SqueezeBoxMode = SqueezeBoxSocket.GetLine
End Function
'**************************************************************
Sub Event_OnPlay
    Dim strRetVal, TrackPath
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If ManualTrackChange = True Then
        slimRequest(formatCommand("""stop"""))
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
    Else
        slimRequest(formatCommand("""playlist"", ""add"", """ + perlEscape(TrackPath) + """"))
       ManualTrackChange = True
   End If
End Sub
'**************************************************************
Function SyncToggle( itm )
    Dim TrackPath
    Dim Mnu
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = False Then
        SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = True
       Call registerEvents
       itm.IconIndex = SDB.RegisterIcon("Scripts\Auto\sbon.ico", 0)
       slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
        slimRequest(formatCommand("""time"", """ + CStr(SDB.Player.PlaybackTime*0.001 + 1) + """"))      'added 1 second to try to make up for net and lookup lag
    Else
       Call unregisterEvents
        slimRequest(formatCommand("""stop"""))
        SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = False
           itm.IconIndex = SDB.RegisterIcon("Scripts\Auto\sboff.ico", 0)
    End If
End Function
'**************************************************************
Function CheckPath(SongValue)
    If Left(SongValue.Path,1) = "?" Then
       If SongValue.Cached Then
           CheckPath = SongValue.CachedPath
       Else
          CheckPath = SongValue.Path
       End If
    Else
       CheckPath = SongValue.Path
   End If
End Function
'**************************************************************
Sub Event_OnStop
    slimRequest(formatCommand("""stop"""))
End Sub
'**************************************************************
Sub Event_OnShutdown
     On Error Resume Next
    slimRequest(formatCommand("""stop"""))
End Sub
'**************************************************************
Sub Event_OnPause
   If SDB.Player.IsPaused Then
       slimRequest(formatCommand("""pause"""))
    Else
        slimRequest(formatCommand("""play"""))
    End If
End Sub
'**************************************************************
Sub Event_OnSeek
    Dim strRetVal, TrackPath
     strRetVal = SqueezeBoxMode
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If InStr(1,strRetval,"mode stop") > 0 Then
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
    End If
    slimRequest(formatCommand("""time"", """ + CStr(SDB.Player.PlaybackTime*0.001) + """"))
End Sub
'**************************************************************
Function slimRequest(params)
    'slimRequest accepts pre-formatted parameter string, returns string of the response'
    'logme "  >> slimRequest: Begin with " + params

      'Dim url : url = "http://" + sbIP + "/jsonrpc.js"
   Dim url : url = "http://" + SDB.IniFile.StringValue("SqueezeBox","IP") & ":9000" + "/jsonrpc.js"
   'Dim prog : Set prog = SDB.Objects("LoadXMLBar")
    Dim prog : Set prog = SDB.Progress
    Dim objHTTP : Set objHTTP = CreateObject("Microsoft.XMLHTTP")
    Call objHTTP.open("POST", url, True)
    objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    objHTTP.setRequestHeader "X-Requested-With", "XMLHttpRequest"
    objHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1"
    objHTTP.send(params)

    Dim cnt : cnt = 0
    While (objHTTP.readyState < 4) And (cnt < 300)
       Call SDB.Tools.Sleep(100)
        SDB.ProcessMessages
        cnt = cnt+1
       If prog.Terminate Then
          cnt = 300
       End If   
    WEnd
    If objHTTP.readyState < 4 Then
       Set slimRequest = Nothing
    Else
       Dim str : str = objHTTP.responseText
          slimRequest = str
      End If
End Function
'*************************************************************
'This is is a JSON RPC call - We're directly calling functions in the Slim::Request module in Perl. See here:
'http://svn.slimdevices.com/repos/slim/7.5/trunk/server/Slim/Control/Request.pm
'the addDispatch section tells you how to use the commands and what the RPC module is looking for in terms of formatting
Function formatCommand(str1)
    Dim str2
    'str2 = "{""id"":1,""method"":""slim.request"",""params"":[""" + sbMAC + """,[" + str1 + "]]}"
   str2 = "{""id"":1,""method"":""slim.request"",""params"":[""" + SDB.IniFile.StringValue("SqueezeBoxx","MAC") + """,[" + str1 + "]]}"
   formatCommand = str2
End Function
'*************************************************************
'borrowed from a teknojunky - if you want the popups add them to this and then just call logme
Sub logme(msg)
     Dim fso, logf
    On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set logf = fso.OpenTextFile(Script.ScriptPath&".log",ForAppending,True)
    logf.WriteLine Now() & ": " & msg
    Set fso = Nothing
    Set logf = Nothing
End Sub
'*************************************************************
'Perl is taking the JSON strings directly into variables - "\" is a special character in perl to escape the following string
'wherever we have a "\" we need it to be "\\" in anything that goes to Perl (ie: anything that goes to Squeezebox)
Function perlEscape(str1)
      str1 = Replace(str1, "\", "\\")
    perlEscape = str1
End Function
'*************************************************************
Sub registerEvents
    Call Script.RegisterEvent(SDB,"OnPlay","Event_OnPlay")
    Call Script.RegisterEvent(SDB,"OnPause","Event_OnPause")
    Call Script.RegisterEvent(SDB,"OnStop","Event_OnStop")
    Call Script.RegisterEvent(SDB,"OnSeek","Event_OnSeek")
End Sub
'*************************************************************
Sub unregisterEvents
     Script.UnregisterEvents SDB
End Sub
'*************************************************************
'These are what the full JSON strings look like that need to be sent to slimRequest()
'formatRequest() fills in the first part and all you have to do is pick the RPC commands from http://svn.slimdevices.com/repos/slim/7.5/trunk/server/Slim/Control/Request.pm

'Note: there are cleaner ways of doing this - but this is quick and dirty. It just means that you have to use a lot of """ and etc in vbscript.
'This entire script is much easier to write in jscript but I don't know jscript at all so, shrug.

'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["status","-",1,"tags:cgABbehldiqtyrSuoKLN"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["status","-",1,"tags:uB"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["playlist","index",28]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["pause"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",[""playlist"", ""play"", ""D:\\MP3\\filename.mp3""]]}


'You can do ANYTHING you can do from the web interface via this RPC engine because you are directly calling the methods in Perl
'so you could add whole playlists and etc see the docs. Sky's the limit.
Last edited by Gingernut63 on Wed Jun 27, 2012 3:29 am, edited 3 times in total.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development

Postby mccstumble » Wed Aug 24, 2011 7:01 pm

What a day today has been. That's the last time I update, in one go, my full Squeezebox and companion programs (ie SBS to LMS, MM Beta1.414 to 1422, Squeezeplay and last, but by no means least, the great MonkeySqueeze form a stable release to a beta.

To be honest most of the time today was spent on Logitech Media Server not starting. It turns out that the install routine doesn't clean out the previous SBS cache which then prevents LMS from starting. Odd they don't include that option in the install?? It's all working fine now.

Have been using the Beta MonkeySqueeze from the last post. Major improvements are (in no particular order), not needing W3 Sockets, no longer having autostart (it was a pain anyway), the enable/disable icons are a real bonus and easy to use with minimum of mouse clicking. Not sure at this stage, as MM has only been playing for an hour or so, but there have been no premature ending of tracks in that time (note; I use MP3 format and not FLAC or any Lossless files).

So, from someone who used the cobbled together script and who is now using the beta script a big thanks to gingernut63 and boobler for this latest beta version of MonkeySqueeze.

I'm not sure how realistic this suugestion is.........MM quite happily plays CDs. LMS has a CD Player Plugin that enables it to play CDs both in RAW format and transcoded. I wonder if it is possible that MonkeySqueeze somehow utilises the CDPlayer Plugin in LMS?

cheers mccstumble
Music & Video Management: Mediamonkey Ver 4.0.0.1436Gold Lifetime
Music Server: Logitech Media Server (LMS) 7.7.0-33512
AddOns: MonkeySqueeze Ver 2.0 http://www.mediamonkey.com/forum/viewtopic.php?f=2&t=59515
On Demand Music: LastFM, Spotify via LMS
mccstumble
 
Posts: 44
Joined: Fri Jul 30, 2010 8:46 am

Re: MonkeySqueeze Development-Buffering Issue

Postby Gingernut63 » Thu Aug 25, 2011 5:26 am

Probably the greatest issue MonkeySqueeze faces is the buffering issue between MediaMonkey and the Squeezebox system. Solve this issue and the basic goals are achieved.

Before I go further I should state I'm writing this from a perspective of a person who is not a MediaMonkey or Squeezebox expert but a person who has a more than basic understanding of computers.

What is the problem? When MediaMonkey is linked to Squeezebox via MonkeySqueeze there is a noticeable lag in the music playing on a Squeezebox device. The lag usually ranges from 1 to 7 seconds depending on file size, type etc... When a new track starts playing on MediaMonkey the previous track can still be playing on the Squeezebox device and it will abruptly finish to be replaced by the next piece of music. This is exacerbated by tracks that have no lead out silence such as some classical, opera, progressive rock etc... any music with gap less tracks.

What is happening is a delay caused by the track being loaded by a Squeezebox device before it plays (buffering) and delays in MediaMonkey communication as it passes through various computer subsystems. The format of the track can have a significant impact on the issue; when playing mp3 tracks the lag is about 1 to 3 seconds however when playing lossless tracks such as FLAC the lag can be from 4 seconds to 10 seconds in extreme cases.

What can be done? Good question. I'm sorry to say at this stage I do not have the skills to do the advanced programming that would be required :( and hence why I'm writing this post. I have a couple of ideas but they may not be achievable or totally off track but I'll table them anyway.

1. If MonkeySqueeze could monitor the track countdown time on a squeezebox device then it could delay the next track from playing until the time falls to 0.00 seconds. A delay between tracks will possibly still occur but it is better than a track abruptly stopping

2. Introduce a synchronise function similar to the one in Squeezebox Server

If anyone has better ideas or wishes to contribute to the discussion please take the time to reply.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development - Beta110827

Postby Gingernut63 » Fri Aug 26, 2011 4:52 pm

Latest Beta release V2.0.0.110827.

This release contains contains changes to the settings page but no changes to the operation of the script. Text gives better connection instructions.

To use this version, copy the script to Notepad and save as MonkeySqueeze.vbs to one one of the following locations:
C:\Program Files\MediaMonkey\Scripts\Auto or C:\Program Files (x86)\MediaMonkey\Scripts\Auto

If you use this script please report any issues on this forum. Feedback is required so we can implement changes before any official release.

Code: Select all
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'
' MonkeySqueeze V2-0-0-110827Beta
'
' ORIGINAL AUTHOR: Todd Nemeth/revel
' DATE STARTED: 26.09.2007
'
' ADDITIONAL AUTHORS: Baz, Big_Berny, Peke, trixmoto, debacle, mccstumble, Gingernut63, booblers
' UPDATE DATE: XX.XX.20XX
'
' COMMENT:
'  - You will also need Squeezebox Server and SqueezePlay installed and running.
'  - After installation the interface for this script is located at Tools\Options\MonkeySqueeze
'
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Option Explicit
Public SqueezeBoxSocket
Public ManualTrackChange
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim onMonkeySqueezeIcon
Dim offMonkeySqueezeIcon
    'Note - you need to include the port here
'Dim sbIP : sbIP = "XXX.XXX.XXX.XXX:9000"
'You can get this mac address manually and hardcode it as I have here or you could make another JSON call to "status" and get the MAC address.
'it will be required to directly send commands to a particular player - which is what you want to do. It is in every call to the player.
'I changed my address below for security reasons - you will need to input yours below.
'Dim sbMAC: sbMAC = "XX:XX:XX:XX:XX:XX"
'**************************************************************
Sub OnStartup
     Dim ind
     Dim UI : Set UI = SDB.UI

      Dim Mnu
   
      onMonkeySqueezeIcon = SDB.RegisterIcon("Scripts\Auto\sbon.ico", 0)
      If onMonkeySqueezeIcon < 0 Then
         onMonkeySqueezeIcon = 74 'unlocked icon if it doesn't exist
      End If
   
      offMonkeySqueezeIcon = SDB.RegisterIcon("Scripts\Auto\sboff.ico", 0)
      If offMonkeySqueezeIcon < 0 Then
         offMonkeySqueezeIcon = 75 'locked icon if it doesn't exist
      End If

     Set Mnu = UI.AddMenuItem( UI.Menu_TbStandard, 0, 0)
     Mnu.Caption = "MonkeySqueeze"
     Mnu.UseScript = Script.ScriptPath
     Mnu.OnClickFunc = "SyncToggle"
     Mnu.IconIndex = onMonkeySqueezeIcon
     Mnu.Hint = "Enable Disable MonkeySqueeze"
 
     Set SDB.Objects("MonkeySqueezeIcon") = Mnu      'save a reference to it so we can change the icon later.

     If SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = True Then
         Call registerEvents
         Mnu.IconIndex = onMonkeySqueezeIcon
      Else
         Mnu.IconIndex = offMonkeySqueezeIcon
      End If

     ind = SDB.UI.AddOptionSheet("MonkeySqueeze",Script.ScriptPath,"InitSheet","SaveSheet",-2)
     InitSqueezeBox
     ManualTrackChange = True
     
End Sub

'**************************************************************
Sub InitSheet(Sheet)
     Dim oPanel1, oCheck1, oLabelAbout, oPanel2, Edit1, oPanel3, Btn1, Edit2

     Set oPanel1 = SDB.UI.NewGroupBox(Sheet)
      oPanel1.Common.SetRect 10,10,450,75
      oPanel1.Caption = "MonkeySqueeze v2.0.0.110827Beta"
     With SDB.UI.NewLabel(oPanel1)
           .Common.SetRect 15,20,440,40
           .Caption = "This script allows MediaMonkey to play music on a Squeezebox System" & vbNewLine _
           & vbNewLine _
           & "To Enable/Disable MonkeySqueeze, click the button on the toolbar main menu"
     End With
 
     Set oPanel2 = SDB.UI.NewGroupBox(Sheet)
      oPanel2.Common.SetRect 10,95,450,60
      oPanel2.Caption = "User Guide"
 
     With SDB.UI.NewLabel(oPanel2)
           .Common.SetRect 15,25,440,40
           .Caption = "Click the button and then open MonkeySqueeze_UserGuide.pdf"
     End With
 
     Set Btn1 = SDB.UI.NewButton(oPanel2)
     Btn1.Common.SetRect 345,20,80,25
     Btn1.Caption = "User Guide"
     Btn1.Common.Hint = "Click to open the folder where the user guide is located"
     Script.RegisterEvent Btn1, "OnClick", "UserGuide"
   
     Set oPanel3 = SDB.UI.NewGroupBox(Sheet)
      oPanel3.Common.SetRect 10,165,450,165
      oPanel3.Caption = "Logitech Media Server IP and Device MAC Addresses"

     With SDB.UI.NewLabel(oPanel3)
           .Multiline = True
           .Common.SetRect 15,20,420,60
           .Caption = "This script uses the IP address of Logitech Media Server (Squeezebox Server) and the MAC address of the device (Touch, SoftSqueeze, etc...) you wish to connect to. Open the Logitech Media Server Web Control page and then open the Settings page. Under the Information tab find the Server IP Address, e.g. 192.168.0.102 and enter below. Find the device you wish to connect to and enter the MAC address below, e.g. z1:6d:4g:40:1f:32"
    End With

     With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 15,114,70,20
           .Caption = "LMS IP Address:"
     End With
 
     Set Edit1 = SDB.UI.NewEdit(oPanel3)
      Edit1.Common.ControlName = "SqueezeBoxIP"
      Edit1.Common.SetRect 97,110,95,25
      Edit1.Text = SDB.IniFile.StringValue("SqueezeBox","IP")
      If Edit1.Text = "" Then
           Edit1.Text = "XXX.XXX.XXX.XXX"
           SDB.IniFile.StringValue("SqueezeBox","IP") = "XXX.XXX.XXX.XXX"
     End If
      Edit1.Common.Hint = "Enter LMS IP Address here"
   
   With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 215,114,70,20
           .Caption = "Device MAC Address:"
     End With
 
     Set Edit2 = SDB.UI.NewEdit(oPanel3)
      Edit2.Common.ControlName = "SqueezeBoxMAC"
      Edit2.Common.SetRect 322,110,110,25
      Edit2.Text = SDB.IniFile.StringValue("SqueezeBoxx","MAC")
      If Edit2.Text = "" Then
           Edit2.Text = "XX:XX:XX:XX:XX:XX"
           SDB.IniFile.StringValue("SqueezeBoxx","MAC") = "XX:XX:XX:XX:XX:XX"
     End If
      Edit2.Common.Hint = "Enter device MAC Address here"
   
   With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 15,140,420,20
           .Caption = "Press OK and restart the music for the changes to take effect."
   End With
   
   
   'Dim Logo
   'With SDB.UI.NewLabel(Sheet)
           '.Common.SetRect 10,500,420,110
      'Logo = "<img src=""" & script.scriptpath & "\MonkeySqueeze_Logo_Emboss.png"" width=""100"" height=""100"">"
      'Logo = "<img src=""" & script.scriptpath & "\MonkeySqueeze_Logo_Emboss.png"">"
      'Logo = "<img src=""" & sdb.ScriptsPath & "Auto\" & "\MonkeySqueeze_Logo_Emboss.png"" width=""100"" height=""100"">"
      'Logo = "<img src=""" & sdb.ScriptsPath & "\MonkeySqueeze_Logo_Emboss.png"" width=""100"" height=""100"">"
     'End With
   
   
   
      
End Sub
'**************************************************************
Sub SaveSheet(Sheet)
   SDB.IniFile.StringValue("SqueezeBox","IP") = Sheet.Common.ChildControl("SqueezeBoxIP").Text
   SDB.IniFile.StringValue("SqueezeBoxx","MAC") = Sheet.Common.ChildControl("SqueezeBoxMAC").Text
End Sub
'**************************************************************
Sub UserGuide
    On Error Resume Next
   Dim OF, FolderToOpen
   Set OF = CreateObject("Shell.Application")
   FolderToOpen = sdb.ScriptsPath & "Auto\"
   OF.Explore FolderToOpen
   Set OF = Nothing
End Sub
'**************************************************************
Sub InitSqueezeBox
     On Error Resume Next
     Set SqueezeBoxSocket = CreateObject("Socket.TCP")
     SqueezeBoxSocket.DoTelnetEmulation = True
     SqueezeBoxSocket.TelnetEmulation = "TTY"
     SqueezeBoxSocket.Host = SDB.IniFile.StringValue("SqueezeBox","IP") & ":9090"
     SqueezeBoxSocket.Open
End Sub
'**************************************************************
Function SqueezeBoxMode
     On Error Resume Next
     SqueezeBoxMode = ""
   'SqueezeBoxSocket.SendLine "mode ?"
   'SqueezeBoxMode = SqueezeBoxSocket.GetLine
End Function
'**************************************************************
Sub Event_OnPlay
    Dim strRetVal, TrackPath
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If ManualTrackChange = True Then
        slimRequest(formatCommand("""stop"""))
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
    Else
        slimRequest(formatCommand("""playlist"", ""add"", """ + perlEscape(TrackPath) + """"))
       ManualTrackChange = True
   End If
End Sub
'**************************************************************
Function SyncToggle( itm )
    Dim TrackPath
    Dim Mnu
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = False Then
        SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = True
       Call registerEvents
       itm.IconIndex = SDB.RegisterIcon("Scripts\Auto\sbon.ico", 0)
       slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
        slimRequest(formatCommand("""time"", """ + CStr(SDB.Player.PlaybackTime*0.001 + 1) + """"))      'added 1 second to try to make up for net and lookup lag
    Else
       Call unregisterEvents
        slimRequest(formatCommand("""stop"""))
        SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = False
           itm.IconIndex = SDB.RegisterIcon("Scripts\Auto\sboff.ico", 0)
    End If
End Function
'**************************************************************
Function CheckPath(SongValue)
    If Left(SongValue.Path,1) = "?" Then
       If SongValue.Cached Then
           CheckPath = SongValue.CachedPath
       Else
          CheckPath = SongValue.Path
       End If
    Else
       CheckPath = SongValue.Path
   End If
End Function
'**************************************************************
Sub Event_OnStop
    slimRequest(formatCommand("""stop"""))
End Sub
'**************************************************************
Sub Event_OnShutdown
     On Error Resume Next
    slimRequest(formatCommand("""stop"""))
End Sub
'**************************************************************
Sub Event_OnPause
   If SDB.Player.IsPaused Then
       slimRequest(formatCommand("""pause"""))
    Else
        slimRequest(formatCommand("""play"""))
    End If
End Sub
'**************************************************************
Sub Event_OnSeek
    Dim strRetVal, TrackPath
     strRetVal = SqueezeBoxMode
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If InStr(1,strRetval,"mode stop") > 0 Then
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
    End If
    slimRequest(formatCommand("""time"", """ + CStr(SDB.Player.PlaybackTime*0.001) + """"))
End Sub
'**************************************************************
Function slimRequest(params)
    'slimRequest accepts pre-formatted parameter string, returns string of the response'
    'logme "  >> slimRequest: Begin with " + params

      'Dim url : url = "http://" + sbIP + "/jsonrpc.js"
   Dim url : url = "http://" + SDB.IniFile.StringValue("SqueezeBox","IP") & ":9000" + "/jsonrpc.js"
   'Dim prog : Set prog = SDB.Objects("LoadXMLBar")
    Dim prog : Set prog = SDB.Progress
    Dim objHTTP : Set objHTTP = CreateObject("Microsoft.XMLHTTP")
    Call objHTTP.open("POST", url, True)
    objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    objHTTP.setRequestHeader "X-Requested-With", "XMLHttpRequest"
    objHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1"
    objHTTP.send(params)

    Dim cnt : cnt = 0
    While (objHTTP.readyState < 4) And (cnt < 300)
       Call SDB.Tools.Sleep(100)
        SDB.ProcessMessages
        cnt = cnt+1
       If prog.Terminate Then
          cnt = 300
       End If   
    WEnd
    If objHTTP.readyState < 4 Then
       Set slimRequest = Nothing
    Else
       Dim str : str = objHTTP.responseText
          slimRequest = str
      End If
End Function
'*************************************************************
'This is is a JSON RPC call - We're directly calling functions in the Slim::Request module in Perl. See here:
'http://svn.slimdevices.com/repos/slim/7.5/trunk/server/Slim/Control/Request.pm
'the addDispatch section tells you how to use the commands and what the RPC module is looking for in terms of formatting
Function formatCommand(str1)
    Dim str2
    'str2 = "{""id"":1,""method"":""slim.request"",""params"":[""" + sbMAC + """,[" + str1 + "]]}"
   str2 = "{""id"":1,""method"":""slim.request"",""params"":[""" + SDB.IniFile.StringValue("SqueezeBoxx","MAC") + """,[" + str1 + "]]}"
   formatCommand = str2
End Function
'*************************************************************
'borrowed from a teknojunky - if you want the popups add them to this and then just call logme
Sub logme(msg)
     Dim fso, logf
    On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set logf = fso.OpenTextFile(Script.ScriptPath&".log",ForAppending,True)
    logf.WriteLine Now() & ": " & msg
    Set fso = Nothing
    Set logf = Nothing
End Sub
'*************************************************************
'Perl is taking the JSON strings directly into variables - "\" is a special character in perl to escape the following string
'wherever we have a "\" we need it to be "\\" in anything that goes to Perl (ie: anything that goes to Squeezebox)
Function perlEscape(str1)
      str1 = Replace(str1, "\", "\\")
    perlEscape = str1
End Function
'*************************************************************
Sub registerEvents
    Call Script.RegisterEvent(SDB,"OnPlay","Event_OnPlay")
    Call Script.RegisterEvent(SDB,"OnPause","Event_OnPause")
    Call Script.RegisterEvent(SDB,"OnStop","Event_OnStop")
    Call Script.RegisterEvent(SDB,"OnSeek","Event_OnSeek")
End Sub
'*************************************************************
Sub unregisterEvents
     Script.UnregisterEvents SDB
End Sub
'*************************************************************
'These are what the full JSON strings look like that need to be sent to slimRequest()
'formatRequest() fills in the first part and all you have to do is pick the RPC commands from http://svn.slimdevices.com/repos/slim/7.5/trunk/server/Slim/Control/Request.pm

'Note: there are cleaner ways of doing this - but this is quick and dirty. It just means that you have to use a lot of """ and etc in vbscript.
'This entire script is much easier to write in jscript but I don't know jscript at all so, shrug.

'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["status","-",1,"tags:cgABbehldiqtyrSuoKLN"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["status","-",1,"tags:uB"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["playlist","index",28]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["pause"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",[""playlist"", ""play"", ""D:\\MP3\\filename.mp3""]]}


'You can do ANYTHING you can do from the web interface via this RPC engine because you are directly calling the methods in Perl
'so you could add whole playlists and etc see the docs. Sky's the limit.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development

Postby Gingernut63 » Tue Aug 30, 2011 6:44 am

MonkeySqueeze is now on twitter! https://twitter.com/#!/MonkeySqueeze1/

For the latest info on all things MonkeySqueeze, latest releases, news, tips, beta releases and development. A quick way to find out what is happening and telling you when to visit the forums. Feel free to tweet your comments on MonkeySqueeze.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development - Beta 110831

Postby Gingernut63 » Wed Aug 31, 2011 2:34 pm

Latest Beta release V2.0.0.110831.

This release adds the ability to change the HTTP port number if Logitech Media Server (Squeezebox Server) is using a different HTTP port due to conflicts with another program. Also contains changes to the settings page.

To use this version, copy the script to Notepad and save as MonkeySqueeze.vbs to one one of the following locations:
C:\Program Files\MediaMonkey\Scripts\Auto or C:\Program Files (x86)\MediaMonkey\Scripts\Auto

If you use this script please report any issues on this forum. Feedback is required so we can implement changes before any official release.


Code: Select all
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'
' MonkeySqueeze V2-0-0-110831Beta
'
' ORIGINAL AUTHOR: Todd Nemeth/revel
' DATE STARTED: 26.09.2007
'
' ADDITIONAL AUTHORS: Baz, Big_Berny, Peke, trixmoto, debacle, mccstumble, Gingernut63, booblers
' UPDATE DATE: XX.XX.20XX
'
' COMMENT:
'  - You will also need Logitech Media Server (Squeezebox Server) installed and running
'  - Optional: Install and run SqueezePlay or SoftSqueeze
'  - After installation the settings page for this script is located at Tools > Options > MonkeySqueeze
'
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Option Explicit
Public SqueezeBoxSocket
Public ManualTrackChange
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim onMonkeySqueezeIcon
Dim offMonkeySqueezeIcon
   

'**************************************************************
Sub OnStartup
     Dim ind
     Dim UI : Set UI = SDB.UI

      Dim Mnu
   
      onMonkeySqueezeIcon = SDB.RegisterIcon("Scripts\Auto\sbon.ico", 0)
      If onMonkeySqueezeIcon < 0 Then
         onMonkeySqueezeIcon = 74 'unlocked icon if it doesn't exist
      End If
   
      offMonkeySqueezeIcon = SDB.RegisterIcon("Scripts\Auto\sboff.ico", 0)
      If offMonkeySqueezeIcon < 0 Then
         offMonkeySqueezeIcon = 75 'locked icon if it doesn't exist
      End If

     Set Mnu = UI.AddMenuItem( UI.Menu_TbStandard, 0, 0)
     Mnu.Caption = "MonkeySqueeze"
     Mnu.UseScript = Script.ScriptPath
     Mnu.OnClickFunc = "SyncToggle"
     Mnu.IconIndex = onMonkeySqueezeIcon
     Mnu.Hint = "Enable/Disable MonkeySqueeze"
 
     Set SDB.Objects("MonkeySqueezeIcon") = Mnu      'save a reference to it so we can change the icon later.

     If SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = True Then
         Call registerEvents
         Mnu.IconIndex = onMonkeySqueezeIcon
      Else
         Mnu.IconIndex = offMonkeySqueezeIcon
      End If

     ind = SDB.UI.AddOptionSheet("MonkeySqueeze",Script.ScriptPath,"InitSheet","SaveSheet",-2)
     InitSqueezeBox
     ManualTrackChange = True
     
End Sub

'**************************************************************
Sub InitSheet(Sheet)
     Dim oPanel1, oCheck1, oLabelAbout, oPanel2, Edit1, oPanel3, Btn1, Edit2, oPanel4, Edit3

     Set oPanel1 = SDB.UI.NewGroupBox(Sheet)
      oPanel1.Common.SetRect 10,10,450,75
      oPanel1.Caption = "MonkeySqueeze v2.0.0.110831Beta"
     With SDB.UI.NewLabel(oPanel1)
           .Common.SetRect 15,20,440,40
           .Caption = "This script allows MediaMonkey to play music on a Squeezebox System" & vbNewLine _
           & vbNewLine _
           & "To Enable/Disable MonkeySqueeze, click the button on the toolbar main menu"
     End With
 
     Set oPanel2 = SDB.UI.NewGroupBox(Sheet)
      oPanel2.Common.SetRect 10,95,450,60
      oPanel2.Caption = "User Guide"
 
     With SDB.UI.NewLabel(oPanel2)
           .Common.SetRect 15,25,440,40
           .Caption = "Click the button and then open MonkeySqueeze_UserGuide.pdf"
     End With
 
     Set Btn1 = SDB.UI.NewButton(oPanel2)
     Btn1.Common.SetRect 345,20,80,25
     Btn1.Caption = "User Guide"
     Btn1.Common.Hint = "Click to open the folder where the user guide is located"
     Script.RegisterEvent Btn1, "OnClick", "UserGuide"
   
     Set oPanel3 = SDB.UI.NewGroupBox(Sheet)
      oPanel3.Common.SetRect 10,165,450,125
      oPanel3.Caption = "Logitech Media Server IP Address and HTTP Port"

     With SDB.UI.NewLabel(oPanel3)
           .Multiline = True
           .Common.SetRect 15,20,420,50
           .Caption = "Open the LMS (Squeezebox Server) Web Control page > Settings page > Information tab and find the Server IP Address, e.g. 192.168.0.102 and enter below. The default HTTP Port number is 9000. See the user guide if you wish to change it"
    End With

     With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 15,74,70,20
           .Caption = "IP Address:"
     End With
 
     Set Edit1 = SDB.UI.NewEdit(oPanel3)
      Edit1.Common.ControlName = "SqueezeBoxIP"
      Edit1.Common.SetRect 75,70,95,25
      Edit1.Text = SDB.IniFile.StringValue("SqueezeBox","IP")
      If Edit1.Text = "" Then
           Edit1.Text = "XXX.XXX.XXX.XXX"
           SDB.IniFile.StringValue("SqueezeBox","IP") = "XXX.XXX.XXX.XXX"
     End If
      Edit1.Common.Hint = "Enter LMS IP Address here"
   
   With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 215,74,70,20
           .Caption = "HTTP Port No:"
     End With
   
   Set Edit2 = SDB.UI.NewEdit(oPanel3)
      Edit2.Common.ControlName = "SqueezeBoxPort"
      Edit2.Common.SetRect 287,70,35,25
      Edit2.Text = SDB.IniFile.StringValue("SqueezeBox","Port")
      If Edit2.Text = "" Then
           Edit2.Text = "9000"
           SDB.IniFile.StringValue("SqueezeBox","Port") = "9000"
     End If
      Edit2.Common.Hint = "HTTP port default is 9000. See user guide if you wish to change it"
   
   With SDB.UI.NewLabel(oPanel3)
           .Common.SetRect 15,100,420,20
           .Caption = "Press OK and restart the music for the changes to take effect."
   End With
   
   Set oPanel4 = SDB.UI.NewGroupBox(Sheet)
      oPanel4.Common.SetRect 10,300,450,125
      oPanel4.Caption = "Squeezebox Device MAC Address"
   
   With SDB.UI.NewLabel(oPanel4)
           .Multiline = True
           .Common.SetRect 15,20,420,60
           .Caption = "The MAC address of the device (Touch, SqueezePlay, etc...) you wish to connect to. Open the LMS Web Control page > Settings page > Information tab and find the device you wish to connect to. Enter the MAC address below, e.g. z1:6d:4g:40:1f:32"
    End With
 
     With SDB.UI.NewLabel(oPanel4)
           .Common.SetRect 15,74,70,20
           .Caption = "Device MAC Address:"
     End With
 
      Set Edit3 = SDB.UI.NewEdit(oPanel4)
      Edit3.Common.ControlName = "SqueezeBoxMAC"
      Edit3.Common.SetRect 122,70,110,25
      Edit3.Text = SDB.IniFile.StringValue("SqueezeBox","MAC")
      If Edit3.Text = "" Then
           Edit3.Text = "XX:XX:XX:XX:XX:XX"
           SDB.IniFile.StringValue("SqueezeBox","MAC") = "XX:XX:XX:XX:XX:XX"
     End If
      Edit3.Common.Hint = "Enter device MAC Address here"
   
   With SDB.UI.NewLabel(oPanel4)
           .Common.SetRect 15,100,420,20
           .Caption = "Press OK and restart the music for the changes to take effect."
   End With
   
End Sub
'**************************************************************
Sub SaveSheet(Sheet)
   SDB.IniFile.StringValue("SqueezeBox","IP") = Sheet.Common.ChildControl("SqueezeBoxIP").Text
   SDB.IniFile.StringValue("SqueezeBox","Port") = Sheet.Common.ChildControl("SqueezeBoxPort").Text
   SDB.IniFile.StringValue("SqueezeBox","MAC") = Sheet.Common.ChildControl("SqueezeBoxMAC").Text
End Sub
'**************************************************************
Sub UserGuide
    On Error Resume Next
   Dim OF, FolderToOpen
   Set OF = CreateObject("Shell.Application")
   FolderToOpen = sdb.ScriptsPath & "Auto\"
   OF.Explore FolderToOpen
   Set OF = Nothing
End Sub
'**************************************************************
Sub InitSqueezeBox
     On Error Resume Next
     Set SqueezeBoxSocket = CreateObject("Socket.TCP")
     SqueezeBoxSocket.DoTelnetEmulation = True
     SqueezeBoxSocket.TelnetEmulation = "TTY"
     SqueezeBoxSocket.Host = SDB.IniFile.StringValue("SqueezeBox","IP") & ":9090"
     SqueezeBoxSocket.Open
End Sub
'**************************************************************
Function SqueezeBoxMode
     On Error Resume Next
     SqueezeBoxMode = ""
   'SqueezeBoxSocket.SendLine "mode ?"
   'SqueezeBoxMode = SqueezeBoxSocket.GetLine
End Function
'**************************************************************
Sub Event_OnPlay
    Dim strRetVal, TrackPath
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If ManualTrackChange = True Then
        slimRequest(formatCommand("""stop"""))
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
    Else
        slimRequest(formatCommand("""playlist"", ""add"", """ + perlEscape(TrackPath) + """"))
       ManualTrackChange = True
   End If
End Sub
'**************************************************************
Function SyncToggle( itm )
    Dim TrackPath
    Dim Mnu
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = False Then
        SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = True
       Call registerEvents
       itm.IconIndex = SDB.RegisterIcon("Scripts\Auto\sbon.ico", 0)
       slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
        slimRequest(formatCommand("""time"", """ + CStr(SDB.Player.PlaybackTime*0.001 + 1) + """"))      'added 1 second to try to make up for net and lookup lag
    Else
       Call unregisterEvents
        slimRequest(formatCommand("""stop"""))
        SDB.IniFile.BoolValue("MonkeySqueeze","SyncToggle") = False
           itm.IconIndex = SDB.RegisterIcon("Scripts\Auto\sboff.ico", 0)
    End If
End Function
'**************************************************************
Function CheckPath(SongValue)
    If Left(SongValue.Path,1) = "?" Then
       If SongValue.Cached Then
           CheckPath = SongValue.CachedPath
       Else
          CheckPath = SongValue.Path
       End If
    Else
       CheckPath = SongValue.Path
   End If
End Function
'**************************************************************
Sub Event_OnStop
    slimRequest(formatCommand("""stop"""))
End Sub
'**************************************************************
Sub Event_OnShutdown
     On Error Resume Next
    slimRequest(formatCommand("""stop"""))
End Sub
'**************************************************************
Sub Event_OnPause
   If SDB.Player.IsPaused Then
       slimRequest(formatCommand("""pause"""))
    Else
        slimRequest(formatCommand("""play"""))
    End If
End Sub
'**************************************************************
Sub Event_OnSeek
    Dim strRetVal, TrackPath
     strRetVal = SqueezeBoxMode
    TrackPath = CheckPath(SDB.Player.CurrentSong)
    If InStr(1,strRetval,"mode stop") > 0 Then
        slimRequest(formatCommand("""playlist"", ""play"", """ + perlEscape(TrackPath) + """"))
    End If
    slimRequest(formatCommand("""time"", """ + CStr(SDB.Player.PlaybackTime*0.001) + """"))
End Sub
'**************************************************************
Function slimRequest(params)
    'slimRequest accepts pre-formatted parameter string, returns string of the response'
    'logme "  >> slimRequest: Begin with " + params

      'Dim url : url = "http://" + sbIP + "/jsonrpc.js"
   'Dim url : url = "http://" + SDB.IniFile.StringValue("SqueezeBox","IP") & ":9000" + "/jsonrpc.js"
   Dim url : url = "http://" + SDB.IniFile.StringValue("SqueezeBox","IP") + ":" + SDB.IniFile.StringValue("SqueezeBox","Port") + "/jsonrpc.js"
   'Dim prog : Set prog = SDB.Objects("LoadXMLBar")
    Dim prog : Set prog = SDB.Progress
    Dim objHTTP : Set objHTTP = CreateObject("Microsoft.XMLHTTP")
    Call objHTTP.open("POST", url, True)
    objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    objHTTP.setRequestHeader "X-Requested-With", "XMLHttpRequest"
    objHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1"
    objHTTP.send(params)

    Dim cnt : cnt = 0
    While (objHTTP.readyState < 4) And (cnt < 300)
       Call SDB.Tools.Sleep(100)
        SDB.ProcessMessages
        cnt = cnt+1
       If prog.Terminate Then
          cnt = 300
       End If   
    WEnd
    If objHTTP.readyState < 4 Then
       Set slimRequest = Nothing
    Else
       Dim str : str = objHTTP.responseText
          slimRequest = str
      End If
End Function
'*************************************************************
'This is is a JSON RPC call - We're directly calling functions in the Slim::Request module in Perl. See here:
'http://svn.slimdevices.com/repos/slim/7.5/trunk/server/Slim/Control/Request.pm
'the addDispatch section tells you how to use the commands and what the RPC module is looking for in terms of formatting
Function formatCommand(str1)
    Dim str2
    'str2 = "{""id"":1,""method"":""slim.request"",""params"":[""" + sbMAC + """,[" + str1 + "]]}"
   str2 = "{""id"":1,""method"":""slim.request"",""params"":[""" + SDB.IniFile.StringValue("SqueezeBox","MAC") + """,[" + str1 + "]]}"
   formatCommand = str2
End Function
'*************************************************************
'borrowed from a teknojunky - if you want the popups add them to this and then just call logme
Sub logme(msg)
     Dim fso, logf
    On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set logf = fso.OpenTextFile(Script.ScriptPath&".log",ForAppending,True)
    logf.WriteLine Now() & ": " & msg
    Set fso = Nothing
    Set logf = Nothing
End Sub
'*************************************************************
'Perl is taking the JSON strings directly into variables - "\" is a special character in perl to escape the following string
'wherever we have a "\" we need it to be "\\" in anything that goes to Perl (ie: anything that goes to Squeezebox)
Function perlEscape(str1)
      str1 = Replace(str1, "\", "\\")
    perlEscape = str1
End Function
'*************************************************************
Sub registerEvents
    Call Script.RegisterEvent(SDB,"OnPlay","Event_OnPlay")
    Call Script.RegisterEvent(SDB,"OnPause","Event_OnPause")
    Call Script.RegisterEvent(SDB,"OnStop","Event_OnStop")
    Call Script.RegisterEvent(SDB,"OnSeek","Event_OnSeek")
End Sub
'*************************************************************
Sub unregisterEvents
     Script.UnregisterEvents SDB
End Sub
'*************************************************************
'These are what the full JSON strings look like that need to be sent to slimRequest()
'formatRequest() fills in the first part and all you have to do is pick the RPC commands from http://svn.slimdevices.com/repos/slim/7.5/trunk/server/Slim/Control/Request.pm

'Note: there are cleaner ways of doing this - but this is quick and dirty. It just means that you have to use a lot of """ and etc in vbscript.
'This entire script is much easier to write in jscript but I don't know jscript at all so, shrug.

'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["status","-",1,"tags:cgABbehldiqtyrSuoKLN"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["status","-",1,"tags:uB"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["playlist","index",28]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",["pause"]]}
'{"id":1,"method":"slim.request","params":["00:04:20:17:06:xx",[""playlist"", ""play"", ""D:\\MP3\\filename.mp3""]]}


'You can do ANYTHING you can do from the web interface via this RPC engine because you are directly calling the methods in Perl
'so you could add whole playlists and etc see the docs. Sky's the limit.
Last edited by Gingernut63 on Sun Sep 11, 2011 8:17 pm, edited 2 times in total.
MonkeySqueeze – Squeezing music into your life!
https://twitter.com/#!/MonkeySqueeze1
MonkeySqueeze Support: viewtopic.php?f=2&t=59515
MonkeySqueeze Development: viewtopic.php?f=19&t=59907
MediaMonkey user since 2005
Gingernut63
 
Posts: 188
Joined: Thu Jul 14, 2011 4:13 am

Re: MonkeySqueeze Development

Postby Katteman » Fri Sep 02, 2011 3:53 pm

I am happy with the new versions. Just before the last Beta I changed my port from the Synology default 9002 to 9000 (sigh).
I also modified TrackPath to change the MediaMonkey file name to the NAS server file name, and use SqueezeCenter on the NAS.
First change backslash to forward slash.
Then change the base of the PC-based music file system to the base of the NAS music file system. In my case, change "Y:" to "/volume1/music".
Now it works on the NAS server and I don't need to change servers when I change from Squeezebox Controller to MediaMonkey and vice versa.

I hope the time lag problem can be solved, because in my system it is sometimes as large as 15 seconds. Very annoying.

Thanks for the script and all efforts,
Katteman
Katteman
 
Posts: 9
Joined: Fri Sep 02, 2011 2:09 pm

Next

Return to Addons developer forum

Who is online

Users browsing this forum: No registered users and 3 guests