Copyright July 1999, Chuck Rice. All rights reserved. WeatherServerWindow.EnableMenuItems: Sub EnableMenuItems() Configuration.Enabled = true End Sub WeatherServerWindow.Open: Sub Open() dim r as boolean dim rc as integer dim index as integer CRLF = chr(13) + chr(10) bugLog = new debugWindow bugLog.write "MacMorgan Weather Server Startup" ConfigW = new ConfigWindow ConfigW.visible = false 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 "WeatherServer","ServerName",ServerName.text appPrefs.writeitem "WeatherServer","SerialPortName",str(SerialPortName.listindex) appPrefs.writeitem "WeatherServer","H1scale", (ConfigW.H1scale.text) appPrefs.writeitem "WeatherServer","H2scale", (ConfigW.H2scale.text) appPrefs.writeitem "WeatherServer","H1offset",(ConfigW.H1offset.text) appPrefs.writeitem "WeatherServer","H2offset",(ConfigW.H2offset.text) appPrefs.writeitem "WeatherServer","RNScale", (ConfigW.RNScale.text) appPrefs.writeitem "WeatherServer","WSScale", (ConfigW.WSScale.text) appPrefs.writeitem "WeatherServer","WDoffset",(ConfigW.WDoffset.text) appPrefs.writeitem "WeatherServer","T1offset",(ConfigW.T1offset.text) appPrefs.writeitem "WeatherServer","T2offset",(ConfigW.T2offset.text) appPrefs.writeitem "WeatherServer","ATscale", (ConfigW.ATscale.text) appPrefs.putText end if ServerName.text = appPrefs.readItem("WeatherServer","ServerName") SerialPortName.listindex = val(appPrefs.readItem("WeatherServer","SerialPortName")) ConfigW.H1scale.text = appPrefs.readItem("WeatherServer","H1scale") ConfigW.H2scale.text = appPrefs.readItem("WeatherServer","H2scale") ConfigW.H1offset.text = appPrefs.readItem("WeatherServer","H1offset") ConfigW.H2offset.text = appPrefs.readItem("WeatherServer","H2offset") ConfigW.RNscale.text = appPrefs.readItem("WeatherServer","RNscale") ConfigW.WSscale.text = appPrefs.readItem("WeatherServer","WSscale") ConfigW.T1offset.text = appPrefs.readItem("WeatherServer","T1offset") ConfigW.T2offset.text = appPrefs.readItem("WeatherServer","T2offset") ConfigW.ATscale.text = appPrefs.readItem("WeatherServer","ATscale") bugLog.write "Prefs Read" Serial1.SerialPort = System.SerialPort(SerialPortName.listindex) for index = 0 to 10 bugLog.write "Opening socket" + str(index) weatherSocket(index).address = ServerName.text // set the address weatherSocket(index).port = 3333 + index // set the port weatherSocket(index).listen //Log.addrow "Listening " + weatherSocket(index).address + ":" + str(weatherSocket(index).port) // add status to listbox bugLog.write "Listening " + weatherSocket(index).address + ":" + str(weatherSocket(index).port) // add status to listbox next if Log.listCount > 1 then Log.listIndex = Log.listCount - 1 // select the last line in listbox Log.refresh end if bugLog.write "exiting window open" Exception OutOfBoundsException MsgBox "Whoops!" End Sub WeatherServerWindow.Log.Open: Sub Open() Log.heading(0)="" Log.heading(1)="T1" Log.heading(2)="T2" Log.heading(3)="Press" Log.heading(4)="AD" Log.heading(5)="WDir" Log.heading(6)="WGust" Log.heading(7)="WSpeed" Log.heading(8)="Rain" Log.heading(9)="H1" Log.heading(10)="H2" End Sub WeatherServerWindow.ServerName.LostFocus: Sub LostFocus() dim index as integer for index = 0 to 10 weatherSocket(index).close weatherSocket(index).address = ServerName.text // set the address weatherSocket(index).listen Log.addrow "Now listening on " + weatherSocket(index).address + ":" + str(weatherSocket(index).port) // add status to listbox if Log.listCount > 1 then Log.listIndex = Log.listCount - 1 // select the last line in listbox end if next appPrefs.writeitem "WeatherServer","ServerName",ServerName.text appPrefs.putText End Sub WeatherServerWindow.Serial1.DataAvailable: Sub DataAvailable() dim d as date dim s as string dim TAB as string TAB = chr(9) s = serial1.readall s = replaceAll(s, chr(10), " ") s = replaceAll(s, chr(13), " ") //if xraw = "" then //xraw = d.shortdate + TAB + d.longtime + TAB //end if xraw = xraw + s //bugLog.write xraw //beep End Sub WeatherServerWindow.SerialPortName.Open: Sub Open() dim sp as SerialPort dim count,i as integer count = System.SerialPortCount if count <> 0 then for i = 0 to count-1 me.AddRow System.SerialPort(i).Name next end //me.listindex = 0 End Sub WeatherServerWindow.SerialPortName.Change: Sub Change() dim r as boolean Serial1.Close Serial1.SerialPort = System.SerialPort(SerialPortName.listindex) r = Serial1.open if r then 'connected ok! //Log.addrow "Connected to " + SerialPortName.text buglog.write "Connected to " + SerialPortName.text else 'oops, error no connection //Log.addrow "Error, serialport did not respond." buglog.write "Error, serialport did not respond." end if //if Log.listCount > 1 then //Log.listIndex = Log.listCount - 1 // select the last line in listbox //end if if appPrefs <> nil then appPrefs.writeitem "WeatherServer","SerialPortName",str(SerialPortName.listindex) appPrefs.putText end if End Sub WeatherServerWindow.weatherSocket.DataAvailable: Sub DataAvailable(Index As Integer) dim temp,s,sdata as string dim i,j,k,tc as integer bugLog.write "WeatherSocket DataAvailable entered with index " +str(index) if index = 0 then return end if temp = weatherSocket(index).readall // get the available data temp = replaceAll(temp, chr(10), "") // delete the line feeds because they don't display right for i = 1 to countFields(temp, chr(13)) s = uppercase(nthField(temp, chr(13), i)) // add a row for each line in the responce from the server if s = "GETRAW" then weatherSocket(index).write xrawdata + "$" end if if s = "GETDATA" then weatherSocket(index).write xdata + "$" end if if s = "GETHISTORY" then timer1.mode = 0 j = historyPtr sdata = "" for k = 1 to 100 j = (j +1) mod 100 sdata = sdata + history(j) +chr(9) + chr(13) + chr(10) //Log.addrow history(j) next weatherSocket(index).write sdata + "$" timer1.mode = 2 end if next if Log.listCount > 1 then Log.listIndex = Log.listCount - 1 // select the last line in listbox Log.refresh end if Exception OutOfBoundsException MsgBox "Whoops! socket data" End Sub WeatherServerWindow.weatherSocket.Error: Sub Error(Index As Integer) bugLog.write "WeatherSocket Error entered with index " +str(index) + "and error code " + str(me.LastErrorCode) select case me.LastErrorCode case 102 //Log.addrow "Connection " + str(index) + " dropped" weatherSocket(index).close portConnected(index) = "N" //Log.addrow "Socket " + str(index) + " Closed" //Log.listIndex = Log.listCount - 1 // select the last row in the listbox, forces it to scroll down //Log.refresh weatherSocket(index).address = ServerName.text // set the address weatherSocket(index).listen //Log.addrow "Listening " + weatherSocket(index).address + ":" + str(weatherSocket(index).port) // add status to listbox else //Log.addrow "Socket " + str(index) + " Error" +str(me.LastErrorCode) end select bugLog.write "WeatherSocket Error exited with index " +str(index) + "and error code " + str(me.LastErrorCode) Exception OutOfBoundsException MsgBox "Whoops! Socket Error" End Sub WeatherServerWindow.weatherSocket.Connected: Sub Connected(Index As Integer) dim i as integer bugLog.write "WeatherSocket connected entered with index " +str(index) if index = 0 then i = 1 while portConnected(i)="Y" and i < 11 i = i + 1 wend if i < 11 then weatherSocket(0).write "SETPORT " + str(3333 + i) + " $" + CRLF portConnected(i) = "Y" weatherSocket(0).close //Log.addrow "Socket 0 Closed" weatherSocket(0).listen //Log.addrow "Listening " + weatherSocket(i).address + ":" + str(weatherSocket(i).port) // add status to listbox else msgbox "error" return end if end if //Log.addrow "Connection " + str(index) +" opened" // add status to listbox //if Log.listCount > 1 then //Log.listIndex = Log.listCount - 1 // select the last line in listbox //Log.refresh //end if Exception OutOfBoundsException MsgBox "Whoops! Socket Connect" + str(i) End Sub WeatherServerWindow.Timer1.Action: Sub Action() dim d as date dim TAB as string dim H1Scale, H1Offset as double dim H2Scale, H2Offset as double dim WSScale, WGScale as double dim RnScale as double dim timestamp as string TAB = chr(9) H1Scale = val(configW.H1scale.text) H2Scale = 2.3 RnScale = 1 WSScale = 1 WGScale = 1 H1Offset = -262 H2Offset = -262 if Log.ListCount > 100 then beep Log.DeleteAllRows end if d = new date xraw = replaceAll(xraw, " ", " ") //Compress out double blanks xraw = replaceAll(xraw, " ", " ") //Compress out double blanks xraw = replaceAll(xraw, " ", " ") //Compress out double blanks timestamp = d.shortdate + " " + d.longtime + TAB currentData = timestamp + xraw Log.addrow timestamp bugLog.write xraw Log.listIndex = Log.lastIndex if xraw <> "" then if nthField(xraw," ",1) = "T1" then T1.text = format((val(nthField(xraw," ",2)) * 1.8)+32,"#####.0") Log.cell(Log.lastindex,1) = T1.text end if if nthField(xraw," ",3) = "T2" then T2.text = format((val(nthField(xraw," ",4)) * 1.8)+32,"#####.0") Log.cell(Log.lastindex,2) = T2.text end if if nthField(xraw," ",5) = "AT" then AT.text = format((val(nthField(xraw," ",6))),"#####") Log.cell(Log.lastindex,3) = AT.text end if if nthField(xraw," ",7) = "AD" then AD.text = format(val(nthField(xraw," ",8)),"#####") Log.cell(Log.lastindex,4) = AD.text end if if nthField(xraw," ",9) = "WD" then WD.text = format(val(nthField(xraw," ",10)),"#####") Log.cell(Log.lastindex,5) = WD.text end if if nthField(xraw," ",11) = "WG" then WG.text = format(val(nthField(xraw," ",12))*WGScale,"#####") Log.cell(Log.lastindex,6) = WG.text end if if nthField(xraw," ",13) = "WS" then WS.text = format(val(nthField(xraw," ",14))*WSScale,"#####") Log.cell(Log.lastindex,7) = WS.text end if if nthField(xraw," ",15) = "RN" then RN.text = format(val(nthField(xraw," ",16)) *RnScale,"#####") Log.cell(Log.lastindex,8) = RN.text end if if nthField(xraw," ",17) = "H1" then H1.text = format((val(nthField(xraw," ",18)) * H1scale)+H1Offset,"#####") Log.cell(Log.lastindex,9) = H1.text end if if nthField(xraw," ",19) = "H2" then H2.text = format((val(nthField(xraw," ",20)) * H2scale)+H2Offset,"#####") Log.cell(Log.lastindex,10) = H2.text end if end if xdata = timestamp + T1.text + TAB xdata = xdata + T2.text + TAB xdata = xdata + AT.text + TAB xdata = xdata + AD.text + TAB xdata = xdata + WD.text + TAB xdata = xdata + WG.text + TAB xdata = xdata + WS.text + TAB xdata = xdata + RN.text + TAB xdata = xdata + H1.text + TAB xdata = xdata + H2.text historyPtr = (historyPtr +1) mod 100 history(historyPtr) = xdata //save last 100 datapoints xrawdata = xraw xraw = "" serial1.write chr(255) me.period = 60*1000*val(refresh.text) 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 Log.refresh End Sub ConfigWindow.Close: Sub Close() WeatherServerWindow.ConfigW.visible = false bugLog.write "Config Window Close" End Sub ConfigWindow.Open: Sub Open() bugLog.write "Config Window Close" End Sub ConfigWindow.SaveButton.Action: Sub Action() WeatherServerWindow.appPrefs.writeitem "WeatherServer","H1scale",(H1scale.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","H2scale",(H2scale.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","H1offset",(H1offset.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","H2offset",(H2offset.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","RNScale",(RNScale.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","WSScale",(WSScale.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","WDoffset",(WDoffset.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","T1offset",(T1offset.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","T2offset",(T2offset.text) WeatherServerWindow.appPrefs.writeitem "WeatherServer","ATscale",(ATscale.text) WeatherServerWindow.appPrefs.putText WeatherServerWindow.ConfigW.visible = false End Sub ConfigWindow.CancelButton.Action: Sub Action() WeatherServerWindow.ConfigW.visible = false 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 buglog.write "writeitem " + sectionName + " " +itemName 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 buglog.write "readitem " + sectionName + " " +itemName 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 buglog.write "getText " + prefFile 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 buglog.write "putText " 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 debugWindow.write: Sub write(line as string) dim d as date d = new date me.debugLog.addRow d.LongTime + " " + line me.debugLog.ListIndex = me.debugLog.LastIndex me.refresh End Sub debugWindow.Open: Sub Open() //me.write "Debug Mode Activated" me.top = screen(0).height - (me.height+1) me.left = screen(0).left + 10 //me.width = 500 me.visible = true End Sub