Copyright July 1999, Chuck Rice. All rights reserved. WeatherClientWindow.WindDirection: Function WindDirection(n as integer) As String dim d as string if n > 0 then d = "North" end if if n > 90 then d = "East" end if if n > 180 then d = "South" end if if n > 270 then d = "West" end if return d End Function WeatherClientWindow.Close: Sub Close() weatherSocket.close End Sub WeatherClientWindow.Open: Sub Open() dim r as boolean dim rc as integer dim d as date dim timestamp as string CRLF = chr(13) + chr(10) appPrefs= New prefFileItem appPrefs.fileInfo= new folderItem rc=appPrefs.getText("WildRice prefs") if rc=1 then 'create default prefs appPrefs.setCreatorType "smpl","pref" appPrefs.writeitem "WeatherClient","ServerName",ServerName.text appPrefs.writeitem "WeatherClient","Refresh",Refresh.text appPrefs.putText end if ServerName.text = appPrefs.readItem("WeatherClient","ServerName") Refresh.text = appPrefs.readItem("WeatherClient","Refresh") //d = new date //timestamp = d.shortdate + " " + d.longtime //Log.addrow timestamp weatherSocket.address = ServerName.text // set the address //Log.addrow "Attempting connection to " + weatherSocket.address + ":" + str(weatherSocket.port) // add status to listbox //if Log.listCount > 1 then //Log.listIndex = Log.listCount - 1 // select the last line in listbox //Log.refresh //end if weatherSocket.connect //weatherSocket.write "gethistory" + CRLF End Sub WeatherClientWindow.weatherSocket.Error: Sub Error() dim d as date dim timestamp as string d = new date timestamp = d.shortdate + " " + d.longtime Log.addrow timestamp Log.addrow "Error " + str(weatherSocket.LastErrorCode) + " on " + weatherSocket.address + ":" + str(weatherSocket.port) // add status to listbox if Log.listCount > 1 then Log.listIndex = Log.listCount - 1 // select the last line in listbox Log.refresh end if weatherSocket.close End Sub WeatherClientWindow.weatherSocket.DataAvailable: Sub DataAvailable() dim i,j,k as integer dim a(11) as string dim TAB as string TAB = chr(9) indata = indata + weatherSocket.readall // get the available data if instr(indata,"$") = 0 then return end if indata = replaceAll(indata, chr(10), "") // delete the line feeds since they don't display right indata = replaceAll(indata, "$" , "") // delete the line feeds since they don't display right indata = replaceAll(indata, " " , " ") // delete the line feeds since they don't display right xdata = nthField(indata," ",1) if xdata = "SETPORT" then weatherSocket.close weatherSocket.port = val(nthField(indata," ",2)) weatherSocket.address = ServerName.text // set the address weatherSocket.connect indata = "" weatherSocket.write "gethistory" + CRLF return end if if Log.ListCount > 100 then Log.removeRow 0 end if j = 1 xdata = nthField(indata,chr(13),j) while xdata <> "" if nthField(xdata,TAB,1) <> "" then if nthField(xdata,TAB,1) <> Log.cell(Log.ListCount-1,0) then if left(nthField(xdata,TAB,1),15) = left(Log.cell(Log.ListCount-1,0),15) then Log.removeRow Log.ListCount-1 else WDarray(0) = ((WDarray(0)) mod 100)+1 WSarray(0) = ((WSarray(0) ) mod 100)+1 WGarray(0) = ((WGarray(0) ) mod 100)+1 T1array(0) = ((T1array(0) ) mod 100)+1 T2array(0) = ((T2array(0) ) mod 100)+1 end if Log.addrow nthField(xdata,TAB,1) Log.cell(Log.lastindex,1) = nthField(xdata,TAB,2) Log.cell(Log.lastindex,2) = nthField(xdata,TAB,3) Log.cell(Log.lastindex,3) = nthField(xdata,TAB,4) Log.cell(Log.lastindex,4) = nthField(xdata,TAB,5) Log.cell(Log.lastindex,5) = nthField(xdata,TAB,6) Log.cell(Log.lastindex,6) = nthField(xdata,TAB,7) Log.cell(Log.lastindex,7) = nthField(xdata,TAB,8) Log.cell(Log.lastindex,8) = nthField(xdata,TAB,9) Log.cell(Log.lastindex,9) = nthField(xdata,TAB,10) Log.cell(Log.lastindex,10)= nthField(xdata,TAB,11) T1.text = nthField(xdata,TAB,2) T2.text = nthField(xdata,TAB,3) AT.text = nthField(xdata,TAB,4) AD.text = nthField(xdata,TAB,5) WD.text = nthField(xdata,TAB,6) WG.text = nthField(xdata,TAB,7) WS.text = nthField(xdata,TAB,8) WS2.text= nthField(xdata,TAB,8) RN.text = nthField(xdata,TAB,9) H1.text = nthField(xdata,TAB,10) H2.text = nthField(xdata,TAB,11) WDarray(WDarray(0)) = (val(WD.text)) WSarray(WSarray(0)) = (val(WS.text)) WGarray(WGarray(0)) = (val(WG.text)) T1array(T1array(0)) = (val(T1.text)+20) T2array(T2array(0)) = (val(T2.text)+20) WDirection.text = WindDirection(val(wd.text)) end if end if j = j + 1 xdata = nthField(indata,chr(13),j) wend if Log.listCount > 1 then Log.listIndex = Log.listCount - 1 // select the last line in listbox end if Log.refresh T1canvas.refresh T2canvas.refresh WindSpeedCanvas.refresh WindDirCanvas.refresh WSCanvas.refresh indata = "" End Sub WeatherClientWindow.weatherSocket.Connected: Sub Connected() Log.addrow "Connection opened" // add status to listbox if Log.listCount > 1 then Log.listIndex = Log.listCount - 1 // select the last line in listbox end if End Sub WeatherClientWindow.Log.Open: Sub Open() dim i as integer Log.heading(0)="" Log.heading(1)="T1" Log.heading(2)="T2" Log.heading(3)="Pres" Log.heading(4)="AD" Log.heading(5)="Dir" Log.heading(6)="Gust" Log.heading(7)="Speed" Log.heading(8)="Rain" Log.heading(9)="H1" Log.heading(10)="H2" '- ColumnAlignment(column As Integer) As Integer (r/w) '- CellAlignment(row As Integer, cell As Integer) As Integer (r/w) '- Values for the alignment properties are: '- 0 = default alignment '- 1 = Left Alignment '- 2 = Center Alignment '- 3 = Right Alignment '- 4 = Decimal Alignment (using the internationalized Decimal separator) '- ColumnAlignmentOffset(column As Integer) As Integer (r/w) '- CellAlignmentOffset(row As Integer, cell As Integer) As Integer (r/w) '- The alignment offset properties allow the positioning of the text to be adjusted (+right) for i = 1 to 2 Log.ColumnAlignment(i) = 4 Log.ColumnAlignmentOffset(i) = -10 next for i = 3 to 10 Log.ColumnAlignment(i) = 3 Log.ColumnAlignmentOffset(i) = -5 next End Sub WeatherClientWindow.ServerName.LostFocus: Sub LostFocus() beep weatherSocket.close weatherSocket.address = ServerName.text // set the address weatherSocket.Connect Log.addrow "Now connected to " + weatherSocket.address + ":" + str(weatherSocket.port) // add status to listbox if Log.listCount > 1 then Log.listIndex = Log.listCount - 1 // select the last line in listbox end if appPrefs.writeitem "WeatherClient","ServerName",ServerName.text appPrefs.putText End Sub WeatherClientWindow.Timer1.Action: Sub Action() dim d as date d = new date weatherSocket.write "getdata" me.period = val(Refresh.text)*1000 // 60*1000*5 if d.second <> 0 then if d.second > 30 then me.period = me.period + 1000 else //me.period = me.period - d.second*1000 end if end if End Sub WeatherClientWindow.T1Canvas.Paint: Sub Paint(g As Graphics) dim i,j as integer g.forecolor=rgb(75,75,75) // g.drawline 0,T1canvas.height-20,T1canvas.width,T1canvas.height-20 g.drawline 0,T1canvas.height-52,T1canvas.width,T1canvas.height-52 g.drawline 0,T1canvas.height-92,T1canvas.width,T1canvas.height-92 j = T1array(0) g.forecolor=rgb(0,100,0) // Green for i = 1 to 100 g.drawline T1canvas.width-(i*2),T1canvas.height,T1canvas.width-(i*2),T1canvas.height-(T1array(j)) j=j-1 if j < 1 then j = 100 end if next End Sub WeatherClientWindow.WindSpeedCanvas.Paint: Sub Paint(g As Graphics) dim i,x,y,xc,yc as integer g.penwidth = 2 g.penheight = 2 i = val(WD.text) xc = (g.width/2) yc = (g.height/2) x = sin((i-180)*0.017453) * xc y = cos((i-180)*0.017453) * yc g.drawline xc+x,yc+y,xc,yc g.drawoval 0,0,g.height,g.width g.forecolor=rgb(255,255,255) //white g.filloval xc-15,yc-15,30,30 g.forecolor=rgb(0,0,0) //black g.drawoval xc-15,yc-15,30,30 End Sub WeatherClientWindow.T2Canvas.Paint: Sub Paint(g As Graphics) dim i,j as integer g.forecolor=rgb(75,75,75) // Gray g.drawline 0,T1canvas.height-20,T1canvas.width,T1canvas.height-20 g.drawline 0,T1canvas.height-52,T1canvas.width,T1canvas.height-52 g.drawline 0,T1canvas.height-92,T1canvas.width,T1canvas.height-92 j = T2array(0) g.forecolor=rgb(0,100,0) // Green for i = 1 to 100 g.drawline T2canvas.width-(i*2),T2canvas.height,T2canvas.width-(i*2),T2canvas.height-(T2array(j)) j=j-1 if j < 1 then j = 100 end if next //g.forecolor=rgb(100,0,0) // Red //g.drawline T2canvas.width-2,T2canvas.height,T2canvas.width-2,(T2canvas.height-T2array(T2array(0))) End Sub WeatherClientWindow.Refresh.LostFocus: Sub LostFocus() appPrefs.writeitem "WeatherClient","Refresh",Refresh.text appPrefs.putText End Sub WeatherClientWindow.WindDirCanvas.Paint: Sub Paint(g As Graphics) dim i,j,k as integer dim c as color g.forecolor=rgb(75,75,75) // g.drawline 90/4,0, 90/4,T1canvas.height g.drawline 180/4,0,180/4,T1canvas.height g.drawline 270/4,0,270/4,T1canvas.height j = WDarray(0) for i = 1 to 100 k = WDarray(j)/4 if k > 0 then c = RGB(0,100,0) if j = WDarray(0) then c = RGB(100,0,0) end if g.pixel(k+0,i+1) = c g.pixel(k+1,i+1) = c g.pixel(k+0,i+0) = c g.pixel(k+1,i+0) = c end if j=j+1 if j > 100 then j = 1 end if next End Sub WeatherClientWindow.WSCanvas.Paint: Sub Paint(g As Graphics) dim i,j,k as integer g.forecolor=rgb(75,75,75) // //g.drawline 0,WScanvas.height-52,WScanvas.width,WScanvas.height-52 //g.drawline 0,WScanvas.height-92,WScanvas.width,WScanvas.height-92 k = 0 for i = 1 to 100 k = max(k,WSarray(i)) k = max(k,WGarray(i)) next if k < 10 then //k = 10 end if for i = 0 to 4 WSLabel(i).text = str((k/4)*(4-i)) g.drawline 0,WScanvas.height-(i*25),WScanvas.width,WScanvas.height-(i*25) next k = 100/(k) j = WSarray(0) g.forecolor=rgb(0,100,0) // Green for i = 1 to 100 g.forecolor=rgb(75,75,75) // g.drawline WScanvas.width-(i),WScanvas.height,WScanvas.width-(i),WScanvas.height-(WGarray(j)*k) g.forecolor=rgb(100,0,0) // Green g.drawline WScanvas.width-(i),WScanvas.height,WScanvas.width-(i),WScanvas.height-(WSarray(j)*k) j=j-1 if j < 1 then j = 100 end if next End Sub --------------------------------------------------------------------------------- Note: the prefFile Routines were written by someone else. Unfortunatly, I do not remember where I got them. If you know, I would like to find out. --------------------------------------------------------------------------------- prefFileItem.writeItem: Sub writeItem(sectionName as string, itemName as string, itemValue as string) 'this sub will either update the value of itemName with itemValue if it exists 'or it will create itemName and assign itemValue to it if it doesn't exist 'if the sectionName does not exist, that will be created also dim temp, itemText as string dim sectionHeading,itemLabel as String dim sectionPos,nextSectionPos,itemPos,itemEndPos as integer dim headingLen,itemsLen,labelLen as integer dim beforeItem, afterItem as string temp=me.fileText sectionHeading="["+sectionName+"]" headingLen=len(sectionHeading)+1 'add 1 for return at end of line itemLabel=itemName+"=" labelLen=len(itemLabel) sectionPos=instr(temp,sectionHeading) if sectionPos>0 then nextSectionPos=instr(sectionPos+headingLen,temp,"[") if nextSectionPos<>0 then itemsLen=nextSectionPos-(sectionPos+headingLen) itemText=mid(temp,sectionPos+headingLen,itemsLen) itemPos=instr(itemText,itemLabel) itemEndPos=instr(itemPos,itemText,CHR(13)) else 'no next section itemText=mid(temp,sectionPos+headingLen) itemPos=instr(itemText,itemLabel) 'itemEndPos=len(itemText)+1 itemEndPos=instr(itemPos,itemText,CHR(13)) end if if itemPos>0 then 'exists 'breaking item string apart to insert itemValue beforeItem=mid(itemText,1,itemPos+labelLen-1) afterItem=mid(itemText,itemEndPos) itemText=beforeItem+itemValue+afterItem else 'item does not exist itemText=itemText+itemName+"="+itemValue+CHR(13) end if 'breaking entire prefs string apart to reinsert the updated itemText beforeItem=mid(temp,1,sectionPos+headingLen-1) if nextSectionPos=0 then afterItem="" else afterItem=mid(temp,nextSectionPos) end if temp=beforeItem+itemText+afterItem me.fileText=temp else 'section not found me.fileText=me.fileText+CHR(13)+sectionHeading+CHR(13)+itemName+"="+itemValue+CHR(13) 'add section & item end if me.saved=false End Sub prefFileItem.readItem: Function readItem(sectionName as string, itemName as string) As String 'returns value for itemName if found, else returns null dim temp, itemText as string dim itemValue As String dim sectionHeading,itemLabel as String dim sectionPos,nextSectionPos,itemPos,itemEndPos as integer dim headingLen,itemsLen,labelLen as integer temp=me.fileText sectionHeading="["+sectionName+"]" headingLen=len(sectionHeading)+1 'add 1 for return at end of line itemLabel=itemName+"=" labelLen=len(itemLabel) sectionPos=instr(temp,sectionHeading) if sectionPos>0 then nextSectionPos=instr(sectionPos+headingLen,temp,"[") if nextSectionPos<>0 then itemsLen=nextSectionPos-(sectionPos+headingLen) itemText=mid(temp,sectionPos+headingLen,itemsLen) itemPos=instr(itemText,itemLabel) itemEndPos=instr(itemPos,itemText,CHR(13)) else 'no next section itemText=mid(temp,sectionPos+headingLen) itemPos=instr(itemText,itemLabel) 'itemEndPos=len(itemText)+1 itemEndPos=instr(itemPos,itemText,CHR(13)) end if if itemPos>0 then 'exists ItemValue=mid(itemText,itemPos+labelLen,itemEndPos-(itemPos+labelLen)) else 'no item to read itemValue="" end if else 'section not found itemValue="" end if return itemValue End Function prefFileItem.deleteItem: Sub deleteItem(sectionName as string, itemName as string) 'deletes itemName and its itemValue if it exists dim temp, itemText as string dim sectionHeading,itemLabel as String dim sectionPos,nextSectionPos,itemPos,itemEndPos as integer dim headingLen,itemsLen,labelLen as integer dim beforeItem, afterItem as string temp=me.fileText sectionHeading="["+sectionName+"]" headingLen=len(sectionHeading)+1 'add 1 for return at end of line itemLabel=itemName+"=" labelLen=len(itemLabel) sectionPos=instr(temp,sectionHeading) if sectionPos>0 then nextSectionPos=instr(sectionPos+headingLen,temp,"[") if nextSectionPos<>0 then itemsLen=nextSectionPos-(sectionPos+headingLen) itemText=mid(temp,sectionPos+headingLen,itemsLen) itemPos=instr(itemText,itemLabel) itemEndPos=instr(itemPos,itemText,CHR(13)) else 'no next section itemText=mid(temp,sectionPos+headingLen) itemPos=instr(itemText,itemLabel) 'itemEndPos=len(itemText)+1 itemEndPos=instr(itemPos,itemText,CHR(13)) end if if itemPos>0 then 'exists 'breaking item string apart to delete item beforeItem=mid(itemText,1,itemPos-1) afterItem=mid(itemText,itemEndPos+1) itemText=beforeItem+afterItem else 'item does not exist 'do nothing end if 'breaking entire prefs string apart to reinsert the updated itemText beforeItem=mid(temp,1,sectionPos+headingLen-1) if nextSectionPos=0 then afterItem="" else afterItem=mid(temp,nextSectionPos) end if temp=beforeItem+itemText+afterItem me.fileText=temp else 'section not found 'do nothing, can't find item in nonexistent section end if me.saved=false End Sub prefFileItem.deleteSection: Sub deleteSection(sectionName as string) 'deletes the entire section and related items under sectionName dim temp, itemText as string dim sectionHeading,itemLabel as String dim sectionPos,nextSectionPos as integer dim headingLen,itemsLen as integer dim beforeItem, afterItem as string temp=me.fileText sectionHeading="["+sectionName+"]" headingLen=len(sectionHeading)+1 'add 1 for return at end of line sectionPos=instr(temp,sectionHeading) if sectionPos>0 then nextSectionPos=instr(sectionPos+headingLen,temp,"[") if nextSectionPos<>0 then itemsLen=nextSectionPos-(sectionPos+headingLen) itemText=mid(temp,sectionPos+headingLen,itemsLen) else 'no next section itemText=mid(temp,sectionPos+headingLen) end if 'breaking entire prefs string apart to delete the itemText if nextSectionPos=0 then beforeItem=mid(temp,1,sectionPos-2) afterItem="" msgBox itemText else beforeItem=mid(temp,1,sectionPos-1) afterItem=mid(temp,nextSectionPos) end if temp=beforeItem+afterItem me.fileText=temp else 'section not found 'nothing to delete end if me.saved=false End Sub prefFileItem.clear: Sub clear() 'reset the fileText property 'still need to putText if you want to clear the file on disk me.fileText="This file created with Pref Maker class. ©D. Zouras 1998"+CHR(13) me.saved=false End Sub prefFileItem.getText: Function getText(prefFile as string) As integer 'reads the folderItem with name prefFile in the systemFolder:PreferencesFolder '0=success 1=file did not exist, created blank dim inStream As TextInputStream dim outStream as textoutputstream dim prefFolder as folderitem dim rc as integer prefFolder=preferencesFolder //assign the path me.fileInfo= getfolderItem(prefFolder.absolutepath+prefFile) //see if we need to create a new prefs file or not if me.fileInfo.exists then inStream=me.fileInfo.OpenAsTextFile me.fileText=inStream.readAll me.fileText=mid(me.fileText,1,len(me.fileText)-1) inStream.close rc=0 else 'create new pref file me.fileText="This file created with Pref Maker class. ©D. Zouras 1998"+CHR(13) outStream=me.fileInfo.CreateTextFile outStream.WriteLine me.fileText outStream.Close rc=1 end me.saved=true return rc End Function prefFileItem.putText: Sub putText() 'writes the text in the fileText property to the folderItem of this object dim outStream As TextOutputStream dim type,creator as string creator=me.fileInfo.MacCreator type=me.fileInfo.MacType outStream=me.fileInfo.CreateTextFile outStream.WriteLine me.fileText outStream.Close setCreatorType creator,type me.saved=true 'just wrote to disk End Sub prefFileItem.setCreatorType: Sub setCreatorType(creator as string,type as string) 'set the creator and type for the prefFile object 'if a parameter is not 4 chars long then no change will be made 'to it, but the other is independently tested if len(creator)=4 then me.fileInfo.MacCreator=creator end if if len(type)=4 then me.fileInfo.MacType=type end if End Sub